Re: Distinguishing objects and arrays?

On 14/12/11 9:31 PM, Anne van Kesteren wrote:
> It's been postponed to see if ECMAScript 6 or so will obviate the need
> via a different API.


I was just thinking about this again, and I think we have two competing 
desires that we can't resolve nicely.  One is the ability to use 
non-Array objects with a "length" property as a sequence.  This lets us 
pass a NodeList, an HTMLCollection or even a jQuery result object to a 
function expecting a sequence<T>.  The other is the need to allow 
non-Array objects to be considered as dictionaries.

So putting aside a hypothetical dictionary<T> type (a string -> T map), 
and just looking at existing dictionaries:

   dictionary A {
     float x;

   interface B {
     void f(A a);
     void f(sequence<long> xs);

If we do

   var obj = { length: 1 };
   obj[0] = 2;

is obj a dictionary with a missing "x" member or is it a sequence<long>? 
  You might think "if it has a 'length' property consider it to be a 
sequence", but then what if the dictionary had a "length" member?

If we *did* have dictionary<T>, and we were using it for an API like the 
one you mention in the first post in this thread, using the presence of 
a "length" member to determine if it's a sequence or not seems even 
worse, as that would prevent us from setting a length="" attribute on an 
element we're creating.

If we remove the ability to have jQuery result objects (or any non-Array 
plain object) be treated as sequences, then I think we can have 
dictionaries and sequences be considered distinguishable, and allow 
overloads based on them.  We can still allow NodeList etc. to be passed 
in, because they can be tested for and will never be treated as 
dictionary types anyway.  (As long as you don't also have NodeList as 
one of the overloads!)  If you had a jQuery result object you would just 
need to call .toArray() on it before passing it into the function.  I 
don't think that's too bad.

Received on Tuesday, 20 December 2011 01:35:28 UTC