On 8/28/2011 2:49 PM, Andrei Alexandrescu wrote:
On 08/28/2011 04:31 PM, dsimcha wrote:
On 8/28/2011 5:18 PM, Mafi wrote:
dynamicCast: Cast a class to another. This should throw something
derived from Error (not Exception) on failure instead of just
returning
null. If you want null, just use the cast operator. Maybe we should
change its name, though. How about checkedCast or downCast?
I think, to!someClass(x) works like you want dynamic cast to work.
(Slaps self in forehead.) You're right, I forgot about that. Never mind.
I think such an addition would be worthwhile. We'd only need
std.conv.reinterpretCast as that's a distinct way to convert types
than to!T. The function would only work if the source and target types
have the same size. Some overloads of it would be @safe (e.g. integral
or floating point target) whereas others won't (e.g. those that forge
pointers or references). Note that reinterpretCast would support
conversions not covered by cast, e.g. converting a chunk of memory to
a class object or one struct to another struct.
Andrei
I don't know about others, but there's no way in hell that I will ever
type "reinterpretCast" in my source code (let alone /import/ std.conv,
just for a cast) unless the IDE was the one doing the typing.