Re: Proposal: anaphoric if and while syntax

2016-09-16 Thread J Decker
On Fri, Sep 16, 2016 at 10:43 AM, J Decker wrote: > I saw it mentioned in several messages earlier about the comma operator as > applied to the let/var statement... I suppose that IS an issue. > > for example : if( let a = 1, b = 0 ) > 1) if( 1, 0 ) the comma operator clears the expression st

Re: Proposal: anaphoric if and while syntax

2016-09-16 Thread J Decker
I saw it mentioned in several messages earlier about the comma operator as applied to the let/var statement... I suppose that IS an issue. for example : if( let a = 1, b = 0 ) 1) if( 1, 0 ) the comma operator clears the expression stack and results with only the value after the comma... so you

Re: Proposal: anaphoric if and while syntax

2016-09-16 Thread J Decker
On Fri, Sep 16, 2016 at 2:43 AM, Bob Myers wrote: > I often find myself wanting to do this in the case of `while`. I've been > writing > > ``` > for (let a; a = someLongCondition();) { doSomethingWith(a); } > ``` > > In that spirit, an oddball proposal: > > ``` > while (let a = someLongCondition(

Re: Proposal: anaphoric if and while syntax

2016-09-16 Thread Bob Myers
I often find myself wanting to do this in the case of `while`. I've been writing ``` for (let a; a = someLongCondition();) { doSomethingWith(a); } ``` In that spirit, an oddball proposal: ``` while (let a = someLongCondition(); a) { ...use a... } if (let a = someLongCondition(); a) { ...use a...

Re: Proposal: anaphoric if and while syntax

2016-09-15 Thread Isiah Meadows
I say let's hold off until JavaScript gets pattern matching support (assuming it does). It's rather limiting otherwise, and the use case IMHO doesn't really merit a new syntax for it. On Thu, Sep 15, 2016, 15:47 Jeremy Martin wrote: > > If yes ... why would anyone write that ? > > I think it wou

Re: Proposal: anaphoric if and while syntax

2016-09-15 Thread Jeremy Martin
> If yes ... why would anyone write that ? I think it would have to be "yes" (and that's probably just a contrived example that doesn't demonstrate the usefulness). Slightly less contrived, I could see the value in this, though. E.g., ``` router.get('/user', (req, res, next) => { if (let use

Re: Proposal: anaphoric if and while syntax

2016-09-15 Thread Andrea Giammarchi
> if( let a = ( let b = 10 ) * 3 > 10 ) I've honestly no idea, at first/quick read, what the hell that would produce. Is `a` going to be just `true` ? 'cause if not, this proposal violates operator precedence. If yes ... why would anyone write that ? On Thu, Sep 15, 2016 at 7:30 PM, J Decker w

Re: Proposal: anaphoric if and while syntax

2016-09-15 Thread J Decker
Why not more generally - allow let/var declarations in expressions? coming from a long and rich C background, I have no issues with the existing mechanisms... but for those languages that do support variable declarations in for loops; I've always wondered why not any expression? if( let a = ( let

Re: Proposal: anaphoric if and while syntax

2016-09-15 Thread Steve Fink
On 09/12/2016 05:32 PM, Danielle McLean wrote: In current ECMAScript, it is legal to place a variable declaration inside the initialiser of a `for` loop, as well as to declare the variable used by a `for...in` or `for...of` loop within the declaring expression: for (let i = 0; i < 5; ++i) c

Re: Proposal: anaphoric if and while syntax

2016-09-15 Thread Kris Siegel
I agree but at the same time those examples simply look like bad coding practices to me and we can't completely hide from those. Since it's new semantics we shouldn't have to worry about it breaking something existing and it would be really easy to do a transpile back into ES5 or ES6 should people

Re: Proposal: anaphoric if and while syntax

2016-09-15 Thread Andy Earnshaw
The main barrier that I can see is that this requires new semantics. At the moment, variable declarations don't have any kind of usable result. If you consider let a = eval('let b = 1'); console.log(a); //-> undefined This sounds like an easy thing to solve, but how about this:

Re: Proposal: anaphoric if and while syntax

2016-09-15 Thread Kris Siegel
I really like the idea of declaring a variable directly in the conditional itself. Many other languages do it. It's certainly not a critical thing (it makes 2 lines of code into 1) but I certainly would like it to happen. Considering it only moves the declaration I'm not convinced it makes code har

Re: Proposal: anaphoric if and while syntax

2016-09-15 Thread Andy Earnshaw
How so? Assignment is already possible in control structures. I think this reads better: ``` if (let a = foo) { ``` Than: ``` let a; //... if (a = foo) { ``` Having the `let` or `const` inside the conditional part clarifies the intent. There's an argument for not doing assignment inside the

Re: Proposal: anaphoric if and while syntax

2016-09-15 Thread Mark Volkmann
For what it's worth, I'm on the side of people that do not want to see assignment statements in control structures. I don't think it is necessary and it results in code that is harder to read. On Wed, Sep 14, 2016 at 8:51 PM, Alan Johnson wrote: > What about `else if`? > > On Sep 14, 2016 9:28 P

Re: Re: Proposal: anaphoric if and while syntax

2016-09-14 Thread Danielle McLean
"Bergi" wrote: > What about `else` blocks, would the variables be available in them as well? No. If the `else` block executes, then the variable's value would be falsy and therefore presumably not useful for further processing. Of course, there are multiple falsy values in ES, but knowing which

Re: Proposal: anaphoric if and while syntax

2016-09-14 Thread Alan Johnson
What about `else if`? On Sep 14, 2016 9:28 PM, "Bergi" wrote: > Danielle McLean wrote: > > variables declared >> using `let` or `const` would be scoped to the individual `if` or `while` >> statement, rather than the containing block. In other words, the above >> syntax >> would be equivalent to

Re: Proposal: anaphoric if and while syntax

2016-09-14 Thread Bergi
Danielle McLean wrote: variables declared using `let` or `const` would be scoped to the individual `if` or `while` statement, rather than the containing block. In other words, the above syntax would be equivalent to the following currently-valid form I ended up writing: { const oldVal

Re: Proposal: anaphoric if and while syntax

2016-09-14 Thread Jordan Harband
For what it's worth, that specific use case would be addressed by https://github.com/tc39/String.prototype.matchAll ;-) On Wed, Sep 14, 2016 at 4:20 PM, Oriol Bugzilla wrote: > I like this proposal. Specially for `while` loops. > > When I want to iterate all matches of a global regex, I always t

Re: Proposal: anaphoric if and while syntax

2016-09-14 Thread Oriol Bugzilla
I like this proposal. Specially for `while` loops. When I want to iterate all matches of a global regex, I always think the variable declaration at the top seems ugly ```js let match; while (match = regex.exec(string)) { // ... } ``` This looks better: ```js while (let match = regex.exec(str

Re: Proposal: anaphoric if and while syntax

2016-09-14 Thread Caitlin Potter
Given that `let x == 10` or `let x === 10` is illegal, it doesn’t seem too bad to allow `if (let x = f(y))`, LexicalDeclaration is visually distinct from EqualityExpression because of the `let` or `const` prefix. > On Sep 14, 2016, at 6:41 PM, Jordan Harband wrote: > > While I like the idea o

Re: Proposal: anaphoric if and while syntax

2016-09-14 Thread Jordan Harband
While I like the idea of making it simpler to restrict the scope of variables, currently a reigning best practice is to never do assignment in conditionals, since it can be an easy typo from `==` or `===`, and because it conflates assignment with expression truthiness, harming readability. This se

Re: Proposal: anaphoric if and while syntax

2016-09-14 Thread Danielle McLean
On 14 September 2016 at 17:58:24, Viktor Kronvall (viktor.kronv...@gmail.com(mailto:viktor.kronv...@gmail.com)) wrote: > Does this really need new semantic interpretation of the syntax? Using the > `Array.prototype` methods `.forEach` and `.map` already mitigates this > problem as far as I can t

Re: Proposal: anaphoric if and while syntax

2016-09-14 Thread Viktor Kronvall
Does this really need new semantic interpretation of the syntax? Using the `Array.prototype` methods `.forEach` and `.map` already mitigates this problem as far as I can tell by having a different bound variable (argument in this case) for each call. I agree that the behavior may be non-intuitive

Re: Proposal: anaphoric if and while syntax

2016-09-13 Thread Dan Peddle
An alternative to the block which you ended up with is to extract that logic to a function, which provides something which can be tested too. Possibly overkill for oneliners like this though. However, writing a lot of code like this myself (get a value, if it's truthy do something, else do other),