D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 8264 - [std.conv.to] constructing conversion doesn't work with alias this
Summary: [std.conv.to] constructing conversion doesn't work with alias this
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: All All
: P2 normal
Assignee: No Owner
URL:
Keywords: pull, rejects-valid
Depends on:
Blocks:
 
Reported: 2012-06-18 22:28 UTC by Kenji Hara
Modified: 2012-06-20 21:05 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Kenji Hara 2012-06-18 22:28:24 UTC
from http://forum.dlang.org/thread/mailman.1606.1340038410.24740.digitalmars-d-learn@puremagic.com#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 Jonathan M Davis 2012-06-18 22:45:19 UTC
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 Kenji Hara 2012-06-18 23:16:52 UTC
(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.
Comment 4 github-bugzilla 2012-06-20 20:59:39 UTC
Commits pushed to master at https://github.com/D-Programming-Language/phobos

https://github.com/D-Programming-Language/phobos/commit/18564f4fa20bd7d3c66e0d37494d4305edb048fd
fix Issue 8264 - [std.conv.to] constructing conversion doesn't work with alias this

https://github.com/D-Programming-Language/phobos/commit/32d9b329057a01b5775c4cfdebc869f86ca0c691
Merge pull request #640 from 9rnsr/fix8264

Issue 8264 - [std.conv.to] constructing conversion doesn't work with alias this