Multiple opCasts

Denis Koroskin 2korden at gmail.com
Mon Aug 25 08:54:05 PDT 2008


On Mon, 25 Aug 2008 01:44:05 +0400, Russell Lewis  
<webmaster at villagersonline.com> wrote:

> Jarrett Billingsley wrote:
>> "dsimcha" <dsimcha at yahoo.com> wrote in message  
>> news:g8po0h$2hen$1 at digitalmars.com...
>
> (snip)
>
>>> On the calling side:
>>>
>>> Foo foo;
>>> int i = cast(int) foo;  //equivalent to int i = foo.opCast!(int)().
>>> float f = cast(float) foo;  //equivalent to float f =  
>>> foo.opCast!(float)()
>>  No offense meant, but this has been proposed multiple times.
>
> I was going to reply and say that this was an exciting and elegant idea,  
> since I hadn't remembered seeing it before.  I guess I will offer  
> congrats to dsimcha, and also to everybody had who suggested it before.
>
> :)
>
> Russ

I am not that excited with exact this solution since templated functions
cannot be virtual. That's why I give my vote to the following one:

class Foo
{
     void opCast(ref float value) {
         value = 42.0f;
     }

     void opImplicitCast(ref int value) {
         value = 42;
     }
}

Foo foo = new Foo();
int bar = value;               // int bar; value.opImplicitCast(bar);
float baz = cast(float)value;  // float baz; value.opCast(baz);



More information about the Digitalmars-d mailing list