An array destructing specification choice
Allen Wirfs-Brock
allen at wirfs-brock.com
Fri Nov 11 15:36:44 PST 2011
On Nov 11, 2011, at 3:17 PM, David Herman wrote:
> Late to the party, but I've brought more booze.
>
> On Nov 5, 2011, at 2:41 PM, Brendan Eich wrote:
>
>> We have:
>>
>> 1. Should an array pattern always query 'length'?
>>
>> 2. If the answer to (1) is "no", then should ... in an array pattern query 'length'?
>>
>> On reflection and at this point in the thread, with your reply in mind, my prefs in order: [no, yes], [no, no]. In no case do I favor [yes]. I'm refutably matching [no, _] :-P.
>
> I feel strongly that the appropriate semantics is [no, yes].
Pretty much the conclusion I also came to:
On Nov 7, 2011, at 8:53 AM, Allen Wirfs-Brock wrote:
> Yes, as I mentioned in another reply array [ ] access doesn't check length. But "slicing" operations including Array.prototype.slice and Function.prototype.apply (slices from 0 to length) all do use length.
>
> That is way [no, yes] may actually be the most consistent approach.
> ...
>
> A pattern of the form
>
> [a0, a1, 〰〰, ak, ...r]
>
> desugars to
>
> a0 = %v[0];
> a1 = %v[1];
> 〰〰
> ak = %v[k];
> let %length = %v.length;
do we sample the length here or at the very beginning? It presumably only matter if a %v[n] is an accessor with side-effects that modify %v. Generally, the array functions sample length at the beginning before processing any elements.
> r = [ %v[i] for i of [k+1, 〰〰, %length - 1] if (i in %v) ];
>
> This can be generalized further to allow a fixed number of patterns *after* the ellipsis as well:
>
> A pattern of the form
>
> [a0, a1, 〰〰, ak, ...r, bn, bn-1, 〰〰, b0]
We currently haven't specified this syntactic form. I'm not sure if it adds enough value to justify the added conceptual complexity.
Allen
More information about the es-discuss
mailing list