Bugzilla – Bug 14188
Last modified: 2011-12-22 09:34:06 UTC
I would like a way to say that an element in an array either has to be a Node or DOMString.
E.g. compound NodeOrDOMString = Node | DOMString;
(In reply to comment #0)
> I would like a way to say that an element in an array either has to be a Node
> or DOMString.
Why? What is the use case?
"compound" is the wrong word -- think of chemical compounds. It means an aggregate, a product type in ML, a struct in C/C++.
What you mean here is "union", but a safe one, I hope! I.e. no way to pun memory containing the bits representing an instance of one type as if they were bits representing an instance of another type. IIRC WebIDL already has an option type like this, for nullability.
Indeed, what's the use-case beyond option types?
The idea is to make appendChild(DOMString) work. It would append a Text node. The other idea is to make appendChild(array) work. What you can put in the array should not be different from what you can put in appendChild().
It would be nice if unions worked inline. Especially for variadic operations.
E.g. append(DOMString | Node... node)
See the end of http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#element for an example in use.
append(DOMString or Node... node)
reads slightly clearer
Union types are not necessary for some simple overloading cases, like the various canvas methods
void drawImage(HTMLCanvasElement e, float x, float y);
void drawImage(HTMLImageElement e, float x, float y);
although they would probably benefit from becoming more readable.
They are necessary for the case that Anne brings up there, specifying the type of the variadic arguments in an operation (unless you want to use "any"). Similarly for the element type of sequence<T> and T.
But union types bring with them the same kinds of problems that overloading does, namely how to select which of the constituent types is meant when you need to convert values.
void f(Node or DOMString x);
Do ToObject and check if it's a Node, or do a ToString and assume it's a DOMString?
See also: http://lists.w3.org/Archives/Public/public-script-coord/2011OctDec/0265.html
Added per http://lists.w3.org/Archives/Public/public-script-coord/2011OctDec/0344.html. Anne, please indicate whether this change is satisfactory.