3 ms·
I really like that for loops can be expressions. It seems obvious in hindsight, but hindsight is always 20/20 :)
by _7bxa 4y ago
I really like that for loops can be expressions. It seems obvious in hindsight, but hindsight is always 20/20 :)
- masklinn 4y ago> It seems obvious in hindsight It's not, because most languages don't have an `else` clause in their for loop (and in my experience with Python that clause is quite confusing so its use is not common). And a for loop can be executed 0 times, so without a mechanism for a fallback it might not have a value to yield.
- Someone 4y ago> And a for loop can be executed 0 times, so without a mechanism for a fallback it might not have a value to yield. I would think that and the similar case where no iteration hits break are solvable by having a for loop return an optional type.
- bjoli 4y agoI think a language where it is an expression should also let the loop accumulate a value. In those cases you could just have it return the identity/base type of whatever you are accumulating. Say like the for loops in racket, or my own loops for guile scheme: https://git.sr.ht/%7Ebjoli/goof-loop https://git.sr.ht/%7Ebjoli/goof-loop