Why doesn't alias this work with arrays?

Andrej Mitrovic andrej.mitrovich at gmail.com
Mon Jun 18 10:05:35 PDT 2012


On 6/18/12, Jonathan M Davis <jmdavisProg at gmx.com> wrote:
> At that point, you'd need to be converting from string[] to Wrap[],
> which would mean creating a new array

It doesn't have to allocate anything because there's the 'alias this'
and a single data member. All the compiler has to do is cast the array
type to Wrap[]. Casting is safe in this case and should be allowed to
be implicit:

void main()
{
    string[] x = ["foo", "bar"];
    Wrap[] y = cast(Wrap[])x;
    assert(y[0] == "foo");  // safe
}

Of course this only works if there are no other data fields in Wrap
except the string, and that's exactly what I have in this case.


More information about the Digitalmars-d-learn mailing list