Why doesn't alias this work with arrays?

Kenji Hara k.hara.pg at gmail.com
Mon Jun 18 22:22:42 PDT 2012


On Monday, 18 June 2012 at 16:51:11 UTC, Andrej Mitrovic wrote:
> struct Wrap
> {
>     string wrap;
>     alias wrap this;
> }
>
> void main()
> {
>     Wrap x;
>     x = "foo";  // ok
>     Wrap[] y = ["foo", "bar"];  // fail
> }
>
> Error: cannot implicitly convert expression (["foo","bar"]) of 
> type
> string[] to Wrap[]
>
> Any special reason why this doesn't work? I hope it's just a 
> bug or
> unfinished implementation.

This kind conversions should be possible with std.conv.to.

import std.conv;
struct Wrap
{
     string wrap;
     alias wrap this;
}
void main()
{
     Wrap[] y = to!(Wrap[])(["foo", "bar"]);  // shold work
}

If you can construct Wrap object with the syntax Wrap("foo"), 
std.conv.to runs 'conversion by construction'.
And if S is convertible to T, std.conv.to!(T[])(S[] source) runs 
'element-wise array conversion'.
As a result, string[] to Wrap[] will be converted.

...but, this does not work in 2.060head, it is a bug.

Kenji Hara


More information about the Digitalmars-d-learn mailing list