www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Why doesn't alias this work with arrays?

reply Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
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.
Jun 18 2012
parent "Kenji Hara" <k.hara.pg gmail.com> writes:
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
Jun 18 2012