Re: [Phix] Language Feature Request: For Each
- Posted by petelomax Dec 13, 2021
- 1182 views
Why "for i"? (It seems redundant.)
Sigh. I already explained this in detail above. Let me try again:
There has to be an index (how else do you suggest it iterates down a sequence?),
so I may as well force you to give it a name, and as a bonus show it in the ex.err,
plus it avoids the almost inevitable "so how do I modify all_keys then?" questions.
I'll take another look at hiding it/making it "optional" when/if ever implemented.
Javascript's forEach() is definitely interesting, but is a method on a class (it seems), so not analogous to Phix's version (it seems).
JavaScript's Array.forEach() is 96% slower than a plain for loop and requires an iterator which is inherently tied to the defining scope, plus there is no String.forEach() method.
(Note that p2js does not use JavaScript Objects at all, since despite their {} syntax they are far closer to dictionaries than Phix sequences, for which it uses [faster] Arrays.)
The Phix method would just be a shorthand anyway, and hence p2js would just slap out a longhand version, should I ever get round to it.