digitalmars.D - Multiple opCasts
- dsimcha (18/18) Aug 23 2008 The discussion on builtin arrays and opImplicitCast has gotten me thinki...
- Jarrett Billingsley (3/23) Aug 23 2008 No offense meant, but this has been proposed multiple times.
- Russell Lewis (7/16) Aug 24 2008 I was going to reply and say that this was an exciting and elegant idea,...
- Denis Koroskin (16/32) Aug 25 2008 I am not that excited with exact this solution since templated functions
- Simen Kjaeraas (5/42) Aug 25 2008 Also proposed before (by me, probably others as well).
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
"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
Jarrett Billingsley wrote:"dsimcha" <dsimcha yahoo.com> wrote in message news:g8po0h$2hen$1 digitalmars.com...(snip)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. :) RussOn 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 24 2008
On Mon, 25 Aug 2008 01:44:05 +0400, Russell Lewis <webmaster villagersonline.com> wrote:Jarrett Billingsley wrote: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);"dsimcha" <dsimcha yahoo.com> wrote in message news:g8po0h$2hen$1 digitalmars.com...(snip)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. :) RussOn 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 25 2008
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:Also proposed before (by me, probably others as well). -- SimenJarrett Billingsley wrote: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);"dsimcha" <dsimcha yahoo.com> wrote in message news:g8po0h$2hen$1 digitalmars.com...(snip)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. :) RussOn 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 25 2008