Short Functions
Brendan Eich
brendan at mozilla.com
Sat May 21 23:53:38 PDT 2011
On May 21, 2011, at 11:45 PM, Bob Nystrom wrote:
> Isn't there also:
>
> 4. Shorter function syntax that allows a single expression body which doesn't require return, but if it has a return, it retains the same semantics as current JS functions.
>
> That was my understanding of how -> functions would work:
>
> log(-> 'hi')()) // 'hi', no return required.
Missing ( before the -> but yeah.
> function foo() {
> let bar = (-> { return 'bar'; })();
> return 'not bar';
> }
>
> log(foo()); // 'not bar'
>
> Isn't that what the current proposal is? I'm not arguing for or against this, just trying to understand our options.
That's accurate. But I discounted arrow functions because to be usable, to have the syntax you show above, requires GLR parsing (if bottom up; top-down may be easier, haven't proven it yet).
I have big doubts about TC39 switching from LR(1). *Even though* left-hand-side expressions and destructuring specs would benefit from the precision.
> Personally, I'm having trouble figuring out what my opinion is on how return (et. al.) should act inside a block lambda. I can see it making sense either way:
>
> function sortByName(people) {
> people.sort {|a, b|
> if (a.name == b.name) return 0;
> if (a.name < b.name) return -1;
> return 1;
> }
> }
Nope, looks like a block (with some |...| at the front), return should act as it does in a block.
Use completion values if you like:
function sortByName(people) {
people.sort {|a, b|
(a.name == b.name) ? 0 : (a.name < b.name) ? -1 : 1;
}
}
or
function sortByName(people) {
people.sort {|a, b|
if (a.name == b.name) 0;
else if (a.name < b.name) -1;
else 1;
}
}
if you like.
> function findProperty(path, property) {
> File.open(path) {|file|
> for (let line in file) {
> let [name, value] = line.split('=');
> if (name == property) return value;
> }
> }
> }
Yup. Looks like a block, return works as it does in a block.
/be
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20110521/a4a4483d/attachment.html>
More information about the es-discuss
mailing list