Multiple opCasts
Jarrett Billingsley
kb3ctd2 at yahoo.com
Sat Aug 23 13:08:06 PDT 2008
"dsimcha" <dsimcha at yahoo.com> wrote in message
news:g8po0h$2hen$1 at 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.
More information about the Digitalmars-d
mailing list