The "opCast" overloading?

Chris Nicholson-Sauls ibisbasenji at gmail.com
Wed Nov 22 21:30:38 PST 2006


Unknown W. Brackets wrote:
> I think the problem with that is simply:
> 
> class C
> {
>     void opCast(out long l);
>     void opCast(out int i);
> }
> 
> C c = new C();
> short s = cast(short) c;
> 
> What happens?  I would personally say, an error.  But someone will 
> always be mad about that.
> 
> -[Unknown]

Well, I'd say an error makes some sense.  Except, this is an explicit cast so one assumes 
the user has some clue -- and if not, they soon will.  So... I would say the int version 
is used, and that possibly a warning about possible data loss is generated when -w is 
given.  Its the same concerns as:

# int foo = 0xf0f0f0f0;
# short bar = cast(short)foo;

Casting always has its dangers.

-- Chris Nicholson-Sauls



More information about the Digitalmars-d mailing list