5 ms·
A year back I dropped a proposal idea at the EcmaScript discussion list, I hope it get's picked up sometime. My idea is that `let`, `var` and `const` return th
by ojosilva 7y ago
A year back I dropped a proposal idea at the EcmaScript discussion list, I hope it get's picked up sometime.
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:
if( let row = await db.findOne() ) {
// row available here
}
// row does not exist here
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:
if x:=foo(); x>50 {
// x is here
}
else {
// x is here too
}
// x is not scoped here
And Perl's
if( ( my $x = foo() ) > 50 ) {
print $x
}
- cpeterso 7y agoYou can also declare variables in conditional statements in C/C++ like https://godbolt.org/z/h0BR8K https://godbolt.org/z/h0BR8K int foo(int); int bar(int x) { if (int y = foo(x)) return 0; return x; }
- snek 7y agoThis has been discussed a few times on IRC, but no one has made a proposal yet afaik.
- cocoflunchy 7y agoAlso see assignment expressions recently adopted in python 3.8: https://www.python.org/dev/peps/pep-0572/ https://www.python.org/dev/peps/pep-0572/
- sbergot 7y agoIn Python things are a bit different because declarations and assignments are ambiguous.
- empyrical 7y agoAlso its addition to Python was controversial, to say the least. (I have been enjoying using it, however)
- masklinn 7y agoThey're not ambiguous, assignments are declarations unless the variable was pre-declared (via global or nonlocal). Assignment was specifically made a statement to avoid the confusion / typo risks of `=` v `==`.
- sbergot 7y agoThey are not ambiguous if you have the whole file in your head. This is why there are keywords like "global" & "nonlocal".
- masklinn 7y ago> They are not ambiguous if you have the whole file in your head. 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".
- untog 7y agoFWIW Swift lets you do this, and it's great. Guard statements even better.
- pault 7y agoIn my opinion this is a terrible idea, since it's very easy (I do it all the time) to accidentally write `if (foo = bar)` instead of `if (foo == bar)`. If that were valid syntax it would be a huge footgun. I'd be onboard with it if it required a different syntax.
- TazeTSchnitzel 7y agoBut JS already allows that.
- ubertaco 7y agoI'm down if you require a let/var/const in front of it: 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 }
- fenomas 7y ago> if (foo = bar()) { // syntax error! That's already an error in strict mode (which would presumably be on for anyone writing bleeding-edge JS).
- AgentME 7y agoIt's only an error in strict mode if there's no variable in scope named foo.
- dotancohen 7y agoAnd what if foo had already been defined in the scope?
- munificent 7y agoThe new one shadows it.
- 7y ago
- shortercode 7y agoI know this doesn't match up entirely with what you want syntax wise, but the following works and I consider it quite elegant. { let row; if (row = await db.findOne()) { // } else { // } }
- ojosilva 7y agoIt's not the same. For instance, you could not use a `const` variable. 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.
- jazzyjackson 7y agoYou could always make it a labelled block statement !
- deleted 7y ago[deleted]
- hombre_fatal 7y agoSeems like pretty trivial bikeshedding. How about just: const user = await db.findOne() if (user) ... else ... Typescript can even narrow the type to null vs. User in each branch block.
- kdmccormick 7y agoNice. I like use of the let/var/const keyword to disambiguate from the accidental `if (x = y)` when intending to use `==`.
- codenirvana 7y agoOnly helps with const. Better use linting to avoid such issues.
- kdmccormick 7y ago> Only helps with const. Can you elaborate?
- wbercx 7y agoIf the `x` in the example was a const, it would throw an error because the code attempts to assign the value of `y` to `x`. You cannot assign a new value to a const.
- kdmccormick 7y agoWhat I'm saying is that you wouldn't accidentally type `let x = y` when you meant to type `x == y`
- ashelmire 7y agoYou can also do this in Ruby, though the scope is different.
- algorithm_dk 7y agoWould the else branch trigger on a falsy value, or on an exception? It's a bit confusing, as it looks like it would handle exceptions on the else branch, even though we know we have to wrap await in try/catch for that. 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/Reference/Statements/with https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe... [1] - https://en.wikipedia.org/wiki/Variable_shadowing https://en.wikipedia.org/wiki/Variable_shadowing