Bug 10635 – Error: cannot use array to initialize S
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-07-13T11:09:00Z
Last change time
2013-11-26T03:40:36Z
Keywords
pull, rejects-valid
Assigned to
nobody
Creator
kozzi11
Comments
Comment #0 by kozzi11 — 2013-07-13T11:09:48Z
struct S {
string str;
static S opCall(string[] v) {
S s;
s.str = v[0];
return s;
}
static S opCall(string[string] v) {
S s;
s.str = v.keys[0];
return s;
}
}
S s = ["getnonce"]; // this works
S s = ["getnonce" : "str"]; // this doesnt work
Comment #1 by yebblies — 2013-11-20T22:19:37Z
Now neither work.
Comment #2 by kozzi11 — 2013-11-21T04:07:28Z
(In reply to comment #1)
> Now neither work.
No it is same as before, but my example was bad. I mean this:
struct S {
string str;
static S opCall(string[] v) {
S s;
s.str = v[0];
return s;
}
static S opCall(string[string] v) {
S s;
s.str = v.keys[0];
return s;
}
}
void main(string[] args) {
S s1 = ["getnonce"]; // this works
S s = ["getnonce" : "str"]; // this doesnt work
}
Comment #3 by kozzi11 — 2013-11-21T04:12:29Z
(In reply to comment #2)
> (In reply to comment #1)
> > Now neither work.
>
> No it is same as before, but my example was bad. I mean this:
>
> struct S {
> string str;
> static S opCall(string[] v) {
> S s;
> s.str = v[0];
> return s;
> }
> static S opCall(string[string] v) {
> S s;
> s.str = v.keys[0];
> return s;
> }
> }
>
> void main(string[] args) {
> S s1 = ["getnonce"]; // this works
> S s = ["getnonce" : "str"]; // this doesnt work
> }
void main(string[] args) {
S s1 = S(["getnonce"]); // this works
S s2 = ["getnonce"]; // this works
S s3 = S(["getnonce" : "str"]); // this works
S s4 = ["getnonce" : "str"]; // this doesnt work but shoud be same as s3
}
Comment #4 by k.hara.pg — 2013-11-26T00:49:31Z
https://github.com/D-Programming-Language/dmd/pull/2882
From issue 7019, at least following code should work via "implicit constructor call" feature.
struct S
{
string str;
this(string[] v) { str = v[0]; }
this(string[string] v) { str = v.keys[0]; }
}
S s1 = ["str"]; // should work
S s2 = ["str" : "val"]; // should work
But, 'static opCall' is an old workaround against constructors, and I don't think we should extend the case it workable. So my pull request does not support the OP code.
Comment #5 by github-bugzilla — 2013-11-26T03:34:11Z