Problem with type-casting to interface arrays

Lionello Lunesu lio at lunesu.remove.com
Mon Mar 26 03:58:55 PDT 2007


Daniel Keep wrote:
> 
> Vladimir Panteleev wrote:
>> On Tue, 20 Mar 2007 08:29:55 +0200, Lionello Lunesu <lio at lunesu.remove.com> wrote:
>>
>>> 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!(..)? :)
>> I agree. Imagine the mess this could accidentally cause in templates.
> 
> U[] arrcast(U, T)(T[] arr)
> {
>     U[] result;
>     result.length = arr.length;
>     foreach( i, a ; arr )
>         result[i] = cast(U)a;
>     return result;
> }
> 
> ...
> 
> I[] i = arrcast!(I)(c);

Great example. Your arrcast can do anything, from class C:I to I and 
from class A to class B.

L.



More information about the Digitalmars-d mailing list