Early spread operator
Also, is it currently specified if rest parameters can have default values? Here's how that might work...
let restDefaults = [2, 3]; let f = function(first, ...rest = restDefaults){} f(1) // first -> 1, rest -> [2]
f(1, 2) // first -> 1, variableLength -> [], last -> 2
And here's how it might work with an early spread operator...
let g = function(first, ...mid = [2], last = 3){} g(1) // first -> 1, mid -> [2], last -> 3
g(1, 2) // first -> 1, mid -> [], last -> 2
g(1, 2, 3) // first -> 1, mid -> [2], last -> 3
Also, why not allow default values for lvalues in array destructuring patterns for symmetry with parameter lists...
// basic case [x , y = "y", z = "z"] = arr;
// if rest parameter default values are allowed [x , y = "y", ...z = ["z1", "z2"]] = arr;
// if early spread operator also allowed [x , ...y = ["y1", "y2"], z = "z"] = arr;
Issues:
Throw if default value is not array-like ? others ?
Thanks, Sean Eagan
Whoops, first code block should have been...
let restDefaults = [2, 3]; let f = function(first, ...rest = restDefaults){} f(1) // first -> 1, rest -> restDefaults
f(1, 2) // first -> 1, rest -> [2]
Thanks, Sean Eagan
Another small error, this...
g(1, 2) // first -> 1, mid -> [], last -> 2
...should have been...
g(1, 2) // first -> 1, mid -> [2], last -> 2
Also, why are parameters with default values required to suffix parameter lists since all parameters already have |undefined| as an implicit default value ?
Thanks, Sean Eagan
Yes, I'm interested in this possibility as well. It'll probably take some careful working-through of the details to figure out exactly when/where this is doable, but it's on my radar.
Thanks,
Also, is it currently specified if rest parameters can have default values?
I'm more skeptical of this one. It's sort of treating empty arrays as falsey, which they aren't. And I've never noticed a need for this. But that might just be the BLUB principle in action. Do you have examples/use cases in mind?
On 01.04.2011 18:37, Sean Eagan wrote:
Why not allow the spread operator to appear earlier (still just once though) in ArrayPatternList in harmony:destructuring? This could be useful when there is interesting stuff at the end and not interesting and/or variable length stuff earlier
// variable length early segment function(first, ...variableLength, last){}
// not interesting early segment function(interesting, ...notInteresting, alsoInteresting){}
// identifier omission may be useful in this case function(interesting, ..., alsoInteresting){}
// array literal forms [first, ...variableLength, last] = arr; [interesting, ...notInteresting, alsoInteresting] = arr; [interesting, ..., alsoInteresting] = arr;
Yeah, I think we need it (at least if to make the destructuting feature, then to make it completely). CoffeeScript has such a middle-destructuring feature also: jashkenas.github.com/coffee-script/#destructuring
Dmitry.
I'm more skeptical of this one. It's sort of treating empty arrays as falsey, which they aren't. And I've never noticed a need for this. But that might just be the BLUB principle in action. Do you have examples/use cases in mind?
Here's a hypothetical use case, hope this isn't stretching too much...
let Pizza = function(...toppings = ["pepperoni"]) { this.toppings = toppings; }
let pepperoni = new Pizza(), hawaiin = new Pizza("pineapple", "ham");
On 04/05/11 08:37, Sean Eagan wrote:
Hi Dave,
I'm more skeptical of this one. It's sort of treating empty arrays as falsey, which they aren't. And I've never noticed a need for this. But that might just be the BLUB principle in action. Do you have examples/use cases in mind?
Here's a hypothetical use case, hope this isn't stretching too much...
let Pizza = function(...toppings = ["pepperoni"]) { this.toppings = toppings; }
let pepperoni = new Pizza(), hawaiin = new Pizza("pineapple", "ham");
That's non-orthogonal. Now you don't have a way to create a Pizza with no toppings.
If you want such defaulting, just do:
let Pizza = function(...toppings) { if (toppings.length == 0) toppings = ["pepperoni"]; this.toppings = toppings; }
Waldemar
Why not allow the spread operator to appear earlier (still just once though) in ArrayPatternList in harmony:destructuring? This could be useful when there is interesting stuff at the end and not interesting and/or variable length stuff earlier
// variable length early segment function(first, ...variableLength, last){}
// not interesting early segment function(interesting, ...notInteresting, alsoInteresting){}
// identifier omission may be useful in this case function(interesting, ..., alsoInteresting){}
// array literal forms [first, ...variableLength, last] = arr; [interesting, ...notInteresting, alsoInteresting] = arr; [interesting, ..., alsoInteresting] = arr;
Thanks, Sean Eagan