Overloading opCast with multiple types

Stewart Gordon smjg_1998 at yahoo.com
Tue Oct 3 11:39:15 PDT 2006


Kristian wrote:
<snip>
> 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),

It's only simpler because the compiler wants to treat opCast just like 
any other identifier.  But that is itself partly down to the fact that 
opCast is an identifier like any other.  As such, you can call it as 
obj.opCast() just like any other function.

> 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.

My impression was that it works only on casting to the exact return type 
of opCast.

> 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)?

Please see my proposal

http://tinyurl.com/p2pn4

It's very similar to your idea, but makes use of "cast", which is 
already a keyword, to be treated differently from ordinary function names.

Stewart.

-- 
-----BEGIN GEEK CODE BLOCK-----
Version: 3.1
GCS/M d- s:-@ C++@ a->--- UB@ P+ L E@ W++@ N+++ o K-@ w++@ O? M V? PS- 
PE- Y? PGP- t- 5? X? R b DI? D G e++++ h-- r-- !y
------END GEEK CODE BLOCK------

My e-mail is valid but not my primary mailbox.  Please keep replies on 
the 'group where everyone may benefit.



More information about the Digitalmars-d mailing list