Martin Thomson:
> I know that this is not good practice, but is there something written
> down somewhere explaining why?

I don’t know if it’s written down somewhere.  Using dictionary types for
IDL attributes is forbidden by the spec, because it would mean that a
new copy of the object would need to be returned each time the property
were accessed.  This is the case for sequence<T> types too, where you
can much more obviously encourage wasteful object creation:

  interface A {
    attribute sequence<double> values;
  };

  for (var i = 0; i < myA.values.length; i++) {
    …
  }

That would create a new JS Array each time around the loop.  With
dictionaries you might access a couple of properties from the return
value of the getter property and have similar issues:

  dictionary D {
    double x;
    doubly y;
  };

  interface A {
    attribute D d;
  };

  Math.sqrt(myA.d.x * myA.d.x + myA.d.y * myA.d.y);

This would create four copies of the JS object for the dictionary.

Another point is that these sequence and dictionary objects can’t be
monitored for changes, so for example you couldn’t write a spec that
required the browser to do something when you assign to myA.d.x since
that’s just a plain data property on the object returned from d.  So for
APIs where you do want to notice property value changes like this,
you’ll need to use interfaces, and for array-ish things we’ve now got 
FrozenArray<T>, which is an array reference type (as opposed to
sequence’s (and dictionaries’) pass-by-value behaviour).

We don’t currently have a type that means “reference to an object that
has a particular shape defined by a dictionary”.  So for now if you
really want an API that allows

  myA.d = { x: 1, y: 2 };

where the A object either immediately, or later, inspects the values on
the object, then you have to use “object” as the type and invoke the
type conversions or do the JS property getting in the spec yourself.

-- 
Cameron McCormack ≝ http://mcc.id.au/
_______________________________________________
dev-platform mailing list
dev-platform@lists.mozilla.org
https://lists.mozilla.org/listinfo/dev-platform

Reply via email to