You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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#:
varenumerator= enumerable.GetEnumerator();try{varx;while(enumerator.MoveNext()){x= enumerator.Current;// ...}}finally{vardisposable= 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:
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 innew MyEnumerable()){}classMyEnumerable{public MyEnumerator GetEnumerator(){returnnew MyEnumerator();}publicclassMyEnumerator{publicboolMoveNext()=>false;publicintCurrent=>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:The text was updated successfully, but these errors were encountered: