W3C home > Mailing lists > Public > public-script-coord@w3.org > October to December 2011

Re: Distinguishing objects and arrays?

From: Cameron McCormack <cam@mcc.id.au>
Date: Tue, 20 Dec 2011 12:34:42 +1100
Message-ID: <4EEFE632.9010506@mcc.id.au>
To: Anne van Kesteren <annevk@opera.com>
CC: public-script-coord@w3.org
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.

OK.

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;
   b.f(obj);

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

This archive was generated by hypermail 2.3.1 : Wednesday, 8 May 2013 19:30:05 UTC