Casts, especially array casts
bearophileHUGS at lycos.com
Wed Feb 24 10:23:58 PST 2010
This comes from a recent small discussion on the D.learn newsgroup, and from an answer by Daniel Keep:
There are many possible kinds of casts in D:
- To interpret a sequence of bits in a different way, like a uint as a float, a double as a fixed array of two ints, a fixed array of 8 bytes into a fixed array of 2 ints, etc, with no need of an intermediate union definition (And sometimes I want to convert a class reference to a different class, currently you can do it with: cast(Foo)cast(Void*)bar).
- To perform a safe dynamic cast between two objects, that can return null. This operation is a bit slow.
- To perform a conversion, for example from double to an int, with truncation. In future I can even want a way to tell the compiler how to perform such N to M conversions, so I can convert an int to its string equivalent, etc.
- To perform a cast between two arrays, where I want to convert (reinterpret) each item, using a true conversion.
- Maybe there are other kinds of casting (like turning an immutable into a mutable, an impure into a pure, arg!, etc).
Conflating so much different things in the same cast() syntax is less unsafe and is less flexible.
In C++ there are 4+1 kinds of casts, they add some complexity. Maybe in D2 just two different casts can be enough:
- A cast that works at compile time only, with no run time penality (the first and last type in my list).
- A cast that has to perform some operations at run time (the other kinds of casts in my list).
I think this is an important enough thing for D2, and it's not a backwards compatible change, it's not an additive change if you want to do it well (otherwise if you want to follow the route used by C++ of just discouraging the usage of the old C cast it can be an additive change, but it increases the language complexity).
If you want to do this in a good way, you can for example use:
- static_cast() or scast() for the purely compile-time ones.
- dcast() or dynamic_cast(), or just cast() for the run-time ones. Such casts are a bit safer, so they can use a shorter name.
Another good thing is to make the cast semantics more tidy, aligning the effects of the casts done on an array literal with the effect of the same cast done on a run-time array (I think they are a bit different now).
More information about the Digitalmars-d