Article about problems & suggestions for D 2.0

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Sun Aug 28 14:49:59 PDT 2011


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


More information about the Digitalmars-d mailing list