On Monday, June 18, 2012 19:05:35 Andrej Mitrovic wrote: > On 6/18/12, Jonathan M Davis <jmdavisp...@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.
Yes, but you never defined such a conversion. With alias this, you told the compiler that when you need to convert a Wrap to a string or a string to Wrap, it should use the wrap member variable as if it were the object. You can't even construct a Wrap from a string. You didn't tell the compiler how. Note that Wrap w = "hello"; doesn't compile. The fact that Wrap doesn't happen to have any other member variable is irrelevant. The only conversion that the compiler knows about is the alias this, and that doesn't take into account the rest of the struct at all. - Jonathan M Davis