Re: [Bug 14188] Union types

On Wed, 14 Dec 2011 05:08:25 +0100, <> wrote:

> --- Comment #7 from 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?

Isn't ToObject unnecessary? If it is a Node, it wouldn't be converted. If  
it's not a Node, it still wouldn't be one after conversion.

I think it should first check if the argument matches one of the host  
object types, second matches one of the primitive types, and third convert  
to the first primitive type.

So for

void f(Node or WindowProxy or float or DOMString x);

f(node) // Node
f(window) // WindowProxy
f(1) // float
f("1") // DOMString
f({toString:function(){return "1"}}) // float

Simon Pieters
Opera Software

Received on Wednesday, 14 December 2011 07:12:39 UTC