Assignment was specifically made a statement to avoid the confusion / typo risks of `=` v `==`.
They're not ambiguous period, python's `=` always performs a local declaration unless overridden via the keywords you mentioned.
> This is why there are keywords like "global" & "nonlocal".
It's the exact opposite of your statement: `global` and `nonlocal` indicate non-local bindings, because by default all bindings are local and you do not need to have "the whole file in your head".
{
let row;
if (row = await db.findOne()) {
//
}
else {
//
}
}Also having "phantom" scope blocks get very nasty to read once you have more involved logic, as the block itself has no implied meaning and the programmer has to walk a few lines into it to get what's going on.
const user = await db.findOne()
if (user) ... else ...
Typescript can even narrow the type to null vs. User in each branch block.Other than saving a few characters (the variable name), I don't see any benefit of this, while it makes code harder to read.
Too bad the `with` [0] keyword has been reserved for crap, it sounds nice (not for this, but maybe for something else).
>> and also requires you to start renaming variables (ie. row1, row2...) due them going over their intended scope
Variable shadowing [1] is a really bad practice that makes it hard for people to collaborate and keep the code sane. Bad habits are not a reason for language changes.
[0] - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
int foo(int);
int bar(int x) {
if (int y = foo(x)) return 0;
return x;
}Can you elaborate?
if (foo = bar()) { // syntax error!
}
if (let foo = bar()) { // works fine
}
if (const foo = bar()) { // also works fine
}
if (var foo = bar()) { // also also works fine
}That's already an error in strict mode (which would presumably be on for anyone writing bleeding-edge JS).
i = 3;
foo = \0;
while ( i-- ) {
if ( foo = resultFromFooApi() ) {
break;
}
sleep 1;
}
return foo;
My idea is that `let`, `var` and `const` return the value(s) being assigned. Basically I miss being able to declare variables in the assertion part of `if` blocks that are scoped only during the `if()` block existence (including `else` blocks).
Something along these lines:
The current alternative is to declare the variable outside the `if()` block, but I believe that is inelegant and harder to read, and also requires you to start renaming variables (ie. row1, row2...) due them going over their intended scope.As previous art, Golang's:
And Perl's