Why not move cast to the standard library?

grauzone none at example.net
Thu Sep 24 10:30:42 PDT 2009


Andrei Alexandrescu wrote:
> downs wrote:
>> Andrei Alexandrescu wrote:
>>> downs wrote:
>>>> With all the neat template tricks we have in 2.0, and since we're
>>>> widely redefining the syntax anyway, why not deprecate the current
>>>> cast syntax and move it into object.d as a library function?
>>>>
>>>> So instead of cast(Foo) bar; you would say cast!Foo(bar); .. save on a
>>>> keyword and demonstrate language power at the same time.
>>>>
>>>> What sez ye?
>>> What would the implementation look like?
>>>
>>> Andrei
>>
>> Unions, and LOTS of static ifs. :)
> 
> Unions won't work for casting class objects and interfaces because those 
> do pointer adjustments. I think cast must be a primitive.

When casting interfaces and objects, the primitive cast just calls into 
runtime (functions like _d_dynamic_cast etc.). I don't see a reason why 
cast implemented as templated function couldn't call those runtime 
functions directly.

> Andrei



More information about the Digitalmars-d mailing list