Open LPeter1997 opened 1 year ago
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;
}
}
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 ofIEnumerable<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:
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:Internals
Eventually, we'll want this to be equivalent to the following - just like C#:
Until we have language elements like try-catch-finally or
as
casts, the following should suffice for the majority of cases: