Problem with type-casting to interface arrays

Vladimir Panteleev thecybershadow at gmail.com
Fri May 11 09:06:33 PDT 2007


On Wed, 21 Mar 2007 02:13:21 +0200, Daniel Keep <daniel.keep.lists at gmail.com> wrote:

> Vladimir Panteleev wrote:
>> 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;
> }

That's the solution I used in the end. What I meant, however, is:

void doTypeCast(T, U)(T from, inout U to)
{
     // perhaps other operations on `from` here
     to = cast(U)from;
     // perhaps other operations on `to` here
}

// with intf. I and class C:I :

C[] carr = ....;
I[] iarr;
// ...
doTypeCast(carr, iarr);  // this will cause a direct typecast from C[] to I[], which will just completely break things

My suggestion was to issue a warning when the user tries to do such broken array typecasts.

-- 
Best regards,
  Vladimir                          mailto:thecybershadow at gmail.com



More information about the Digitalmars-d mailing list