Bug 8264 – [std.conv.to] constructing conversion doesn't work with alias this
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2012-06-18T22:28:00Z
Last change time
2012-06-20T21:05:33Z
Keywords
pull, rejects-valid
Assigned to
nobody
Creator
k.hara.pg
Comments
Comment #0 by k.hara.pg — 2012-06-18T22:28:24Z
from http://forum.dlang.org/thread/[email protected]#post-cetlbrtfhbtunchppikq:40forum.dlang.org
----
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.
Comment #1 by issues.dlang — 2012-06-18T22:45:19Z
Is it really bug? What if you have
struct Wrap
{
string wrap;
int i;
double d;
string s;
alias wrap this;
}
Should Wrap("foo") create the same thing as Wrap("foo", int.init, double.init, string.init)? Or should it not work? Maybe it should work, but it seems a bit funny to me to create a Wrap from just a string considering that the alias doesn't define how to initialize the rest of the object.
Comment #2 by k.hara.pg — 2012-06-18T23:16:52Z
(In reply to comment #1)
> Is it really bug?
Yes. The problem is that declaring alias this is incorrectly matches more than two templates. If you remove 'alias wrap this' from Wrap type, the conversion will succeed.
> What if you have
>
> struct Wrap
> {
> string wrap;
> int i;
> double d;
> string s;
>
> alias wrap this;
> }
>
> Should Wrap("foo") create the same thing as Wrap("foo", int.init, double.init,
> string.init)? Or should it not work? Maybe it should work, but it seems a bit
> funny to me to create a Wrap from just a string considering that the alias
> doesn't define how to initialize the rest of the object.
I also it seems a bit funny, but it should work.
std.conv.to supports constructing conversion based on _syntactic possibilities_ - like range interface: r.empty, r.front, and r.popFront().
I think such limitation provides no benefit.