-
-
Notifications
You must be signed in to change notification settings - Fork 609
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Issue 7019 - implicit constructors are inconsistently allowed #1213
Conversation
| } | ||
| } | ||
|
|
||
| S7019 rt_gs = 2; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unusual indent - vertical align doesn't quite justify it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should also add auto r_gs2 = S7019(1); if not present elsewhere
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unusual indent - vertical align doesn't quite justify it
Will fix.
should also add auto r_gs2 = S7019(1); if not present elsewhere
Auto declaration, which doesn't have an explicit type, is irrelevant with the bug 7019.
|
Fixed indentation in the test suite code. |
|
LGTM |
|
In the 7019 bug report, Andrei points out the reason this enhancement is a bad idea. I think his reasoning is sound and we should go with his judgement. |
|
I think he only implemented the global assignment syntax |
|
Yah, this pull request is actually fine. I'll go ahead and merge it. |
Issue 7019 - implicit constructors are inconsistently allowed
|
It's a major syntax change as previously one can be sure struct S
{
void* p;
this(const S) { }
}
void main()
{
const S cs;
S s = cs; // Error: cannot implicitly convert expression (cs) of type const(S) to S
}Please, add comments about how compiler worked before and how it works no to changelog for such changes as it isn't obvious at all. |
|
Implicit constructor call works just only when:
If all conditions satisfied, the declaration const S cs;
S s = cs; // Error: cannot implicitly convert expression (cs) of type const(S) to SIt does not satisfy the condition 2, and it should call a built-in generated postblit constructor. |
Thanks for the rules.
Thanks, but I know that. I just wanted to show that the rules aren't obvious.
The problem is I don't know how do you define explicit. This is how I define it but it doesn't satisfy the compiler: struct S
{
this(int) { }
}
struct S2
{
S s;
}
void f(S s) { }
void main()
{
S s = 5; // ok, explicit
static assert(!__traits(compiles, f(5))); // ok, imlicit
static assert(!__traits(compiles, { S2 s2 = 5; })); // ok, imlicit
static assert(!__traits(compiles, { S2 s2 = S2(5); })); // ok, imlicit
static assert(!__traits(compiles, { S2 s2 = { 5 }; })); // fails, but also imlicit
static assert(!__traits(compiles, { S2 s2 = { s: 5 }; })); // fails, but also imlicit
} |
Yes, I know such current inconsistencies. On ArrayInitializer and StructInitializer elelemts, the implicit ctor call doesn't work in current. |
Why |
|
S2(5) is not a StructInitializer, it is a StructLiteralExp. So, Both In my brain, followings should be allowed. BigInt[] arr = [1,2]; // [1,2] is ArrayInitializer, so be translated to [BigInt(1), BigInt(2)]
struct S { BigInt n; }
S s = { 1 }; // { 1 } is StructInitializer, so will be translated to { BigInt(1) }
S s = { n:1 }; // same as above, and will be translated to { n:BigInt(1) } |
|
I don't see why you are making such difference between By the way, in my mind: I like you first explicit rule and explicit for me is that "the tipe for which a constructor will be called must be explicitly mentioned" i.e.: BigInt[] arr = [1,2]; // explicit
struct S { BigInt n; }
S s = { 1 }; // implicit, no `BigInt` here |
Hmm, your point out is good. It seems better that StructInitializer don't support it. |
http://d.puremagic.com/issues/show_bug.cgi?id=7019
From the @andralex's reply , we can sure that it is an expected feature.