Inferred Type for Explicit Cast

Steven Schveighoffer via Digitalmars-d digitalmars-d at puremagic.com
Sun Dec 21 05:56:39 PST 2014


On 12/21/14 3:23 AM, Jonathan Marler wrote:

> As for the typed cast, what about when the function argument is changed
> from a byte to a short?  If the initial caller was using cast(byte) and
> doesn't change to cast(short), then you are also creating a bug.  I feel
> like you're going to get different bugs either way.  Both the typed and
> auto-typed cast seem to be just as dangerous.  That's not really an
> argument to have auto-cast, I'm just making an observation.

But as the author, you have examined your code, and determined it's OK 
to have whatever expression you are using cast to a byte. This means you 
are sure it won't exceed the byte range. I don't see how this is a bug.

> 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


More information about the Digitalmars-d mailing list