Inferred Type for Explicit Cast

bearophile via Digitalmars-d digitalmars-d at puremagic.com
Sat Dec 20 02:14:10 PST 2014


Jonathan Marler:

> if we allow return value types to be written as "auto" then it 
> makes since to have cast(auto) as well.

I think "cast(auto)" as I understand in your proposal introduces 
too often undefined situations, because in many cases the 
compiler can't know what's the type to cast to, and guessing is 
not acceptable. So I think it's not a good idea.

But sometimes I have code like this:

void main() {
     int x;
     byte y;
     // ...
     y = cast(typeof(y))x;
}


Here I want to cast x to the type of y to allow the assignment to 
y. This is perhaps an acceptable semantics for "cast(auto)":


void main() {
     int x;
     byte y;
     // ...
     y = cast(auto)x; // OK
}


In that case the inference for the casting type is easy because 
the type of y is already defined. More examples:


void foo(byte z) {}
void main() {
     int x;
     // ...
     auto y = cast(auto)x; // Error
     byte y = cast(auto)x; // OK, but not very useful
     foo(cast(auto)x);     // OK
}

Bye,
bearophile


More information about the Digitalmars-d mailing list