-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve let #3235
Comments
Take a look at #3205 for some add'l comments on |
Radical idea: how about getting rid of let altogether and rolling this into for since we want to make for loop variables behave like they're "autoletted" anyway? That's one less syntactic construct and it seems like you inevitably end up using a for loop to demonstrate why let is useful. |
Ok, ok. (3) is totally intentional; let bindings are "simultaneous" in every language I know that has them. We could make it behave like
but if that's built in to |
All the better to get rid of it ;-) The current let is just syntax for immediately calling an anonymous function, which we would still be able to do. |
But that doesn't (won't) behave the same, for example with respect to |
Doesn't using |
Jeff, you don't have to wrap, somebody already handled this case, but on the other hand the for loop doesn't return the actual value as it stands now either:
BTW: SML and Haskell do the sequential (dependent) binding, FWIW. SML's syntax looks very similar to the one proposed, except they add "val" in front of the value declarations. Haskell's is similar:
I think if "const" implied "local" then let would be in better shape, because
emulates the "dependent let" well enough, then you can still have the existing behavior when it's wanted. Or if we could have "val" as an alias for "const local" then even better. Probably people are already using "val" as variable name though. |
I agree with the idea of having You do have to wrap the right-hand side, because in If people generally seem to want the sequential binding behavior, I'm ok with it. |
Ah - yes I see your point about having to wrap now. The sequential bindings + "in" have gotten a lot of use in the ML's and Haskell, I think it's a tried and true approach and seems to be pretty well liked there. |
It would be a breaking change, but we could make |
I appreciate the creative thinking, but I think making
do something totally different from
is somewhere way beyond unacceptable. |
Yeah, it's probably not a good idea. |
Looking at the possibility of adding
without making |
Other example:
if we wanted that to be a |
Let is not as safe or convenient as it could be. The main issue is that with the current let behavior it is too easy to accidentally reassign variables in an outer scope instead of introducing new bindings.
an outer y instead of creating a local y:
The above is also different from
which would reassign to an outer x as well, though it looks like only a stylistic difference.
It's unclear in both cases above where the let bindings end and where the body starts. An optional "in" before the body would help, see 4).
For example, the below gives no error and reassigns the outer x instead of introducing a new variable x inside the let:
Now changing "const" to "local" does create a new binding, but const either should also or give an error (It would be nice if it implied "local" - I like to declare new unchanging variables with const so variables to be mutated stand out).
A let bound variable cannot depend on previous ones which makes lets much less useful than they could be. We often want to limit scope of all variables involved in a computation being built incrementally.
Lets would be easier to read in many cases if an optional "in" were allowed between the bindings and the body. This would also make it clear the dividing line between the body and the bindings, so the examples in 1) would give errors instead of accidentally overwriting variables in outer scopes:
I believe the optional "in" would also make many lets easier to read:
vs
The text was updated successfully, but these errors were encountered: