Inferred Type for Explicit Cast

Jonathan Marler via Digitalmars-d digitalmars-d at puremagic.com
Sun Dec 21 07:20:44 PST 2014


>> I noticed your other comment about wanting a double-typed 
>> cast. I could
>> see that being useful especially if we did something like this:
>>
>>   When a cast is performed, the source and target type must 
>> match the
>> expected types EXACTLY.
>>
>> int y;
>> ushort x = cast(byte, int)y; // cast from int to byte
>>
>> This should definitely produce an error.  This code could 
>> exist if x was
>> initially a byte and was later changed to a ushort. I think 
>> this feature
>> would make casts alot safer because any time a type was 
>> changed you
>> would get an error saying you need to go update all your 
>> casts. What do
>> you think?
>
> I didn't think of it that way, I would assume that cast(byte, 
> int) would only error if the cast-from type is not int, not if 
> the expression used the result in any way other than a byte.
>
> But it does have appeal.
>
> -Steve

It seems alot of the potential bugs from casting can occur when 
one of the types change.  If we forced the cast operator to match 
the type(s) exactly then it would eliminate all these bugs.


More information about the Digitalmars-d mailing list