[Python-ideas] Javascript Destructuring Assignment

Blake Winton bwinton at latte.ca
Wed Mar 7 01:15:24 CET 2007


(re-sending, because I forgot to subscribe first...)

Delaney, Timothy (Tim) wrote:
> Brett Cannon wrote:
>>> Are you assuming variable-length skips, so that
>>>     [a,,b] = [1,2,3,4,5]   would mean a=1;b=5 ?
>> Yep, that's how I read it.
> I read it differently. I'm think you need to have a comma for each
> skipped element:
>     [a,,,b] = [1,2,3,4,5]
> Anyone with in-depth knowledge, or FireFox 2.0 available to test this?

Oddly enough, I build a Javascript 1.7 interpreter just yesterday...

js> [a,b] = [1,2]
1,2
js> a
1
js> b
2
js> [a,b] = [1,2,3]
1,2,3
js> a
1
js> b
2
js> [a,,b] = [1,2,3,4,5]
1,2,3,4,5
js> a
1
js> b
3
js> [a,,,b] = [1,2,3,4,5]
1,2,3,4,5
js> a
1
js> b
4

which is pretty much what I'ld expect.  Having said that, it does make
it easy to mess up in exactly the way you messed up in your example...

Also, for the curious:
js> [a,[b,c],d] = [1,2,3,4]
1,2,3,4
js> a
1
js> b
js> c
js> d
3

I'ld far prefer it to throw an error if you tried to unpack into a
sequence of the wrong length, but that's not the Javascript way, I
suppose...

Later,
Blake.




More information about the Python-ideas mailing list