| Thread overview | |||||||
|---|---|---|---|---|---|---|---|
|
August 23, 2008 Multiple opCasts | ||||
|---|---|---|---|---|
| ||||
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)()
| ||||
August 23, 2008 Re: Multiple opCasts | ||||
|---|---|---|---|---|
| ||||
Posted in reply to dsimcha | "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. | |||
August 24, 2008 Re: Multiple opCasts | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Jarrett Billingsley | 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 | |||
August 25, 2008 Re: Multiple opCasts | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Russell Lewis | 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); | |||
August 25, 2008 Re: Multiple opCasts | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Denis Koroskin | 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 | |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply