polterguy / lizzie

A script language for .Net and the CLR
https://ainiro.io/
MIT License
190 stars 27 forks source link

Each should use a new binding context #15

Closed SapphireBrand closed 5 years ago

SapphireBrand commented 5 years ago

The current implementation of Functions.Each requires that the given symbol is not defined anywhere, which is not possible to locally verify. That is, there is valid Lizzie code that can suddenly fail to work because it is employed in a new context where the iterator variable is now bound.

This requirement also precludes the conventional usage of @i as a iterator variable in loops.

The alternative is to push a new binding context (pushStack) for holding the iterator variable, which seems much more like what happens with loops in conventional languages. (Like C#.)

polterguy commented 5 years ago

Hmm, I did consider this, but kind of argued that creating a new context every time I create a new lambda section {}, would be too expensive. Also, I tried to keep as close to the (arguably faulty) convention used in JavaScript, which keeps the scope at the "function level".

Although I see your point here, and it would certainly be "cleaner" to do it C#'ish style, I am not sure if I want to change this, since it would break too much existing code out there ...

... since the only smart way to do this, would be to impose a new stack on every "scope" (start and end of {}) ...

Does it make sense ...?

I'll keep it open none the less, but I must admit I am slightly more reluctant to doing this change, than all your other suggestions ...

polterguy commented 5 years ago

Sorry, this one is going to need more thinking before I start fiddling with it. Simply too much risk to be able to do during a "late night coding session". However, I definitely see your points here, and they are valid - But this would create a lot of breaking code if I were to implement it "correctly", since the only sane way to implement this, would be to create it such that every scope/lambda object gets its own stack context ...

I'll leave it open as a TODO reminder for me to have a closer look at later though ...