Problem with type-casting to interface arrays

Lionello Lunesu lio at lunesu.remove.com
Mon Mar 19 23:29:55 PDT 2007


Derek Parnell wrote:
> On Mon, 19 Mar 2007 07:52:15 +0200, Vladimir Panteleev wrote:
> 
>> Hi,
>>
>> With an interface I and an implementation C : I, is this code sane?
>>
>>      C[] c = [new C()];
>>      I[] i = cast(I[]) c;
>>
>> Currently, any attempts to do this typecast fail for me (the objects
>> inside i are corrupted/unusable).
>> I don't see why it should fail (per the spec), so I'm inclined to
>> believe it's a compiler bug.
> 
> The thing is that "cast(I)" and "cast(I[])" do different things.
> 
> The code ...
>    I[] i = cast(I[]) c;


Maybe it would be a good idea to add a separate cast expression for 
reinterpret_cast. The cast(..) can do anything from a C-cast to 
dynamic_cast to static_cast to reinterpret_cast. All the safe casts can 
use cast(..) but I think that for conversions without any link between 
the two types, it would be better to introduce some new cast 
syntax/keyword.. cast!(..)? :)

L.



More information about the Digitalmars-d mailing list