Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Foreach loop #124

Open
LPeter1997 opened this issue Jul 29, 2023 · 1 comment
Open

Foreach loop #124

LPeter1997 opened this issue Jul 29, 2023 · 1 comment
Labels
Language idea One single idea which may be a start of a design document Syntax This issue is about syntax

Comments

@LPeter1997
Copy link
Member

LPeter1997 commented Jul 29, 2023

Even in very basic sample code, the lack of a foreach loops is really painful. I don't think it would hurt the language, if for now we define the exact same structure for foreach loops as C# does, basing them off of IEnumerable<T>. Even if we'll come up with our own abstractions later, we likely want to stay compatible to them.

Syntax

I'd like to propose the following syntax:

for (x: int32 in enumerable) { ... }

I believe that with decent range-construction mechanisms, there won't be need for an old-style for-loop anymore, so I propose we use the shorter keyword.

The declared iterator variable is implicitly val, it can not be re-bound. The type annotation is completely optional and can be inferred:

for (x in enumerable) { ... }

Internals

Eventually, we'll want this to be equivalent to the following - just like C#:

var enumerator = enumerable.GetEnumerator();
try
{
    var x;
    while (enumerator.MoveNext())
    {
        x = enumerator.Current;
        // ...
    }
}
finally
{
    var disposable = enumerator as IDisposable;
    disposable?.Dispose();
}

Until we have language elements like try-catch-finally or as casts, the following should suffice for the majority of cases:

var enumerator = enumerable.GetEnumerator();
var x;
while (enumerator.MoveNext())
{
    x = enumerator.Current;
    // ...
}
@LPeter1997 LPeter1997 added Language idea One single idea which may be a start of a design document Syntax This issue is about syntax labels Jul 29, 2023
@jl0pd
Copy link

jl0pd commented Jul 30, 2023

exact same structure for foreach loops as C# does, basing them off of IEnumerable

C# uses duck typing sometimes and foreach is one of cases. Compiler doesn't look for IEnumerable, it looks for type that have accessible instance or extension method called GetEnumerator, returning type that have method MoveNext: () -> bool and property Current

Following compiles:

foreach (int x in new MyEnumerable())
{
}

class MyEnumerable
{
    public MyEnumerator GetEnumerator()
    {
        return new MyEnumerator();
    }
    
    public class MyEnumerator
    {
        public bool MoveNext() => false;
        public int Current => 0;
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Language idea One single idea which may be a start of a design document Syntax This issue is about syntax
Projects
None yet
Development

No branches or pull requests

2 participants