Arrow functions and return values
Le 29/11/2012 09:41, Jussi Kalliokoski a écrit :
It's a bit unclear to me how arrow functions react to semicolons, for example:
var a = (c) => { var b = 2; b * c; }
a(4);
To me, it seems like this should return undefined. After all, the last statement in the function is empty. To actually return b * c, you should drop the semicolon:
var a = (c) => { var b = 2; b * c }
a(4);
I initially only read the 2 snippets and saw no difference. I realized the dropped semicolon only after reading the text in the middle. Making a semantic difference out of a present or missing semicolon seems it would cause a lot of wasted time debugging a program. Especially in JavaScript where semicolons are optional. JS devs have their eyes trained to not worry about semicolons (which is probably why I didn't see a difference at first).
I would expect 8 to be returned in both cases, semicolon or not.
This would be consistent with, for example, Rust and would help avoid annoying accidental returns (see [1] for discussion about this wrt CoffeeScript).
I wish JavaScript kept being consistent with JavaScript.
Jussi Kalliokoski wrote:
It's a bit unclear to me how arrow functions react to semicolons, for example:
var a = (c) => { var b = 2; b * c; }
a(4);
To me, it seems like this should return undefined. After all, the last statement in the function is empty.
Not by the grammar.
You would need a second ; after b * c; to spell the empty statement.
To actually return b * c, you should drop the semicolon:
var a = (c) => { var b = 2; b * c }
a(4);
This would be consistent with, for example, Rust and would help avoid annoying accidental returns (see [1] for discussion about this wrt CoffeeScript).
That's a fine thing in languages like Rust without ASI, which also have type-checking needs not in JS. JS has ASI and no type-checking, so no-go.
Cheers, Jussi
CoffeeScript has no way to avoid implicit return, but JS has 'function' and always will.
On Thu, Nov 29, 2012 at 10:32 AM, Brendan Eich <brendan at mozilla.com> wrote:
You would need a second ; after b * c; to spell the empty statement.
Interesting fact actually, it would mean the empty statement is no longer a NOOP. It can actually alter a program. I can't think of a situation where this is the case in es5, not counting preventing syntactical errors. But with implicit return values, the empty statement could make the difference.
Not sure if this is a problem, just noting that it seems to be a change, one that'll be hard to debug for once implicit returns are heavily used. OTOH people might be tempted to "optimize" functions by adding an empty statement at the end. Or something.
It's a bit unclear to me how arrow functions react to semicolons, for example:
var a = (c) => { var b = 2; b * c; }
a(4);
Hmmm... I was under the impression that arrow functions with normal function bodies do not implicitly return anything. Maybe I need to adjust my spec goggles, but I don't see that in the latest draft.
Kevin Smith wrote:
It's a bit unclear to me how arrow functions react to semicolons, for example: var a = (c) => { var b = 2; b * c; } a(4);
Hmmm... I was under the impression that arrow functions with normal function bodies do not implicitly return anything. Maybe I need to adjust my spec goggles, but I don't see that in the latest draft.
Oh (and d'oh!) you are quite right. There's no implicit return or other TCP aspect save lexical-|this|, at all.
On Thu, Nov 29, 2012 at 8:49 AM, Kevin Smith <khs4473 at gmail.com> wrote:
It's a bit unclear to me how arrow functions react to semicolons, for
example:
var a = (c) => { var b = 2; b * c; }
a(4);
Hmmm... I was under the impression that arrow functions with normal function bodies do not implicitly return anything. Maybe I need to adjust my spec goggles, but I don't see that in the latest draft.
This is correct.
var a = (c) => { var b = 2; b * c; }
a(4); // undefined
On Thu, Nov 29, 2012 at 9:41 AM, Brendan Eich <brendan at mozilla.org> wrote:
Kevin Smith wrote:
It's a bit unclear to me how arrow functions react to semicolons, for example: var a = (c) => { var b = 2; b * c; } a(4);
Hmmm... I was under the impression that arrow functions with normal function bodies do not implicitly return anything. Maybe I need to adjust my spec goggles, but I don't see that in the latest draft.
Oh (and d'oh!) you are quite right. There's no implicit return or other TCP aspect save lexical-|this|, at all.
Sorry for the echo :(
On Thu, Nov 29, 2012 at 7:42 PM, Rick Waldron <waldron.rick at gmail.com>wrote:
On Thu, Nov 29, 2012 at 9:41 AM, Brendan Eich <brendan at mozilla.org> wrote:
Kevin Smith wrote:
It's a bit unclear to me how arrow functions react to semicolons, for example: var a = (c) => { var b = 2; b * c; } a(4);
Hmmm... I was under the impression that arrow functions with normal function bodies do not implicitly return anything. Maybe I need to adjust my spec goggles, but I don't see that in the latest draft.
Oh (and d'oh!) you are quite right. There's no implicit return or other TCP aspect save lexical-|this|, at all.
Oh, I hadn't realized this! In that case, great!
It's a bit unclear to me how arrow functions react to semicolons, for example:
var a = (c) => { var b = 2; b * c; }
a(4);
To me, it seems like this should return undefined. After all, the last statement in the function is empty. To actually return b * c, you should drop the semicolon:
var a = (c) => { var b = 2; b * c }
a(4);
This would be consistent with, for example, Rust and would help avoid annoying accidental returns (see [1] for discussion about this wrt CoffeeScript).
Cheers, Jussi
[1] jashkenas/coffee-script#2477