www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Multiple opCasts

reply dsimcha <dsimcha yahoo.com> writes:
The discussion on builtin arrays and opImplicitCast has gotten me thinking,
why shouldn't it be possible to have more than one opCast (explicit cast)
definition?  Yes, it's impossible to overload a function by return type.
However, since the type that's being cast to is known at compile time and
explicitly specified in the cast syntax, it could easily be done with template
specializations.  For example, on the struct side:

struct Foo {
    T opCast(T : float)() {
        return 3.14159265;
    }

    T opCast(T : int)() {
        return 1;
    }
}

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)()
Aug 23 2008
parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"dsimcha" <dsimcha yahoo.com> wrote in message 
news:g8po0h$2hen$1 digitalmars.com...
 The discussion on builtin arrays and opImplicitCast has gotten me 
 thinking,
 why shouldn't it be possible to have more than one opCast (explicit cast)
 definition?  Yes, it's impossible to overload a function by return type.
 However, since the type that's being cast to is known at compile time and
 explicitly specified in the cast syntax, it could easily be done with 
 template
 specializations.  For example, on the struct side:

 struct Foo {
    T opCast(T : float)() {
        return 3.14159265;
    }

    T opCast(T : int)() {
        return 1;
    }
 }

 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.
Aug 23 2008
parent reply Russell Lewis <webmaster villagersonline.com> writes:
Jarrett Billingsley wrote:
 "dsimcha" <dsimcha yahoo.com> wrote in message 
 news:g8po0h$2hen$1 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
Aug 24 2008
parent reply "Denis Koroskin" <2korden gmail.com> writes:
On Mon, 25 Aug 2008 01:44:05 +0400, Russell Lewis  
<webmaster villagersonline.com> wrote:

 Jarrett Billingsley wrote:
 "dsimcha" <dsimcha yahoo.com> wrote in message  
 news:g8po0h$2hen$1 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);
Aug 25 2008
parent "Simen Kjaeraas" <simen.kjaras gmail.com> writes:
On Mon, 25 Aug 2008 17:54:05 +0200, Denis Koroskin <2korden gmail.com>  
wrote:

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

 Jarrett Billingsley wrote:
 "dsimcha" <dsimcha yahoo.com> wrote in message  
 news:g8po0h$2hen$1 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);
Also proposed before (by me, probably others as well). -- Simen
Aug 25 2008