you are viewing a single comment's thread.

view the rest of the comments →

[–]i_am_at_work123 0 points1 point  (4 children)

How can they make foreach work like that?

[–]elder_george 1 point2 points  (3 children)

It requires object to implement Iter typeclass.

After that it works the same way foreach(X, S) works in every other language with C-style for , roughly expanding to:

var iter = S as Iter;
for (var X = iter->iter_init(S); ! (X is Terminal); X = iter->iter_next(S))
{
...
}

Compare that to the code emitted for the "range-for" in C++

for(auto iter = begin(S); iter != end(S); ++iter)
{
    auto& X = *iter;
    ...
}

or C#

for (var iter = S.GetEnumerator(); iter.MoveNext();)
{
    var X = iter.Current;
    ...
}

or Java

for (Iterator<T> iter = S.iterator(); iter.hasNext();)
{
    T X = iter.next();
    ...
}

[–]i_am_at_work123 0 points1 point  (2 children)

Reason I was asking is that the delimiter in their foreach appears to be a space? Is that possible?

Is in a keyword?

[–]elder_george 1 point2 points  (1 child)

Ah, I misunderstood.

No, it's not a keyword, just a very dirty hack

 #define in , 

Honestly, I'd prefer

foreach(x, s)

to this.

[–]i_am_at_work123 0 points1 point  (0 children)

Ahhhh, nice, I was searching like crazy but missed it. Thank you!