News for hackers. Stuff that matters.
Re:Lily Programming Language by noblethrasher on Friday July 01, 2016 @04:08PM (#12019279)
I can’t tell if you’re referring to iterators (abstracted by `yield return`) or enumerators (abstracted by `foreach`).

In case it’s the latter, I’ll just point out that you can have zero-cost abstraction because, in C#, `foreach` is pattern-based: An object is enumerable with respect to `foreach` whenever:

1. It has a public method named “GetEnumerator” of type ‘E

2. 'E has a method named “MoveNext” of type () -> bool and a property named “Current” of type () -> ’T (where ’T can be any class (including those that inherit from System.ValueType.. i.e. a struct)).

While it is the case that `yield return` does result in allocation for a reference type, it’s also true that this is easily remedied should it become a problem: In my experience, it only takes about 5-10 minutes to implement a zero-cost iterator as I described above.

Many of the BSL collection classes (e.g. System.String (as an IEnumerable<char>) and System.Collections.Generic.List<T>) use the zero-cost abstraction.

There are no comments on this comment yet.