Bug 14188 - Union types
Union types
Status: VERIFIED FIXED
Product: WebAppsWG
Classification: Unclassified
Component: WebIDL
unspecified
All All
: P2 normal
: ---
Assigned To: Cameron McCormack
public-webapps-bugzilla
:
Depends on:
Blocks:
  Show dependency treegraph
 
Reported: 2011-09-17 08:05 UTC by Anne
Modified: 2011-12-22 09:34 UTC (History)
4 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Anne 2011-09-17 08:05: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;
Comment 1 Olli Pettay 2011-09-17 13:16:48 UTC
(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?
Comment 2 Brendan Eich 2011-09-17 16:52:03 UTC
"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?

/be
Comment 3 Anne 2011-09-17 21:19:00 UTC
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().
Comment 4 Anne 2011-11-24 16:04:15 UTC
It would be nice if unions worked inline. Especially for variadic operations.

E.g. append(DOMString | Node... node)
Comment 5 Anne 2011-12-05 13:57:23 UTC
See the end of http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#element for an example in use.
Comment 6 Simon Pieters 2011-12-05 14:14:12 UTC
I think

append(DOMString or Node... node)

reads slightly clearer
Comment 7 Cameron McCormack 2011-12-14 04:08:24 UTC
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?
Comment 9 Cameron McCormack 2011-12-22 06:35:32 UTC
Added per http://lists.w3.org/Archives/Public/public-script-coord/2011OctDec/0344.html.  Anne, please indicate whether this change is satisfactory.