Overloading opCast with multiple types

Kristian kjkilpi at gmail.com
Sun Oct 1 14:55:24 PDT 2006


On Sun, 01 Oct 2006 01:25:15 +0300, Reiner Pope  
<reiner.pope at REMOVE.THIS.gmail.com> wrote:

> As we all know, opCast can only have one overload per class, because D  
> doesn't support overloading by return type. That is a really silly  
> limitation, in my opinion.
[snip]

I think it's not important to consider how overloadable opCasts can be  
implemented.

The documentation indeed states "Since functions cannot be overloaded  
based on return value, there can be only one opCast per struct or class.",  
but that's not the actual reason why there is only one opCast. It may be a  
practical reason (which makes compiler implementation simplier), but it's  
not a physical restriction preventing multiple opCasts. The compiler could  
very well support the following:

class Obj {
     int opCast();
     float opCast();
}

It just depends on how one makes the compiler parse source files.

So, first we should discuss if there should be more than one opCast, and  
should there be implicit type conversion. How should it work in ambigious  
cases. Is that a reason why there is only one opCast? Or is it because  
implicit conversion makes code harder to read (it's less informative)?



More information about the Digitalmars-d mailing list