You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The following program does not compile:
void main() {
struct S {
uint value;
~this() { }}const S a = S(12);S b = a;
}
test.d(10): Error: cannot implicitly convert expression a of type const(S) to S
The reason is that the context pointer stored in a is const, and thus implies that the context it points to is also const. This cannot be copied to the non-const context pointer in b.
This context pointer is not, however, actually treated as const. The following code compiles and passes:
unittest {
int i = 0;
struct S {
int n;
void fun() const {
i++;
}
}
const S s;
assert(i == 0);
s.fun();
assert(i == 1);
}
Full discussion thread at https://forum.dlang.org/thread/p7lp2b$1jod$1@digitalmars.com
I would argue that the correct solution is to allow the assignment.
The text was updated successfully, but these errors were encountered:
Related: https://forum.dlang.org/post/rxolioihwfrljpfjfmxw@forum.dlang.org
// dmd v2.081.1
unittest {
struct S {
uint value;
~this() pure {
}
}
S makeS() pure
{
S s = S();
return s;
}
// Error: cannot implicitly convert expression makeS() of type S to immutable(S)
immutable S s = makeS();
}
Without the destructor compiles fine.
Perhaps a better error message could be found.
Further related issue: https://issues.dlang.org/show_bug.cgi?id=18567
Shachar Shemesh (@Shachar) reported this on 2018-03-06T14:47:23Z
Transferred from https://issues.dlang.org/show_bug.cgi?id=18563
CC List
Description
The following program does not compile: void main() { struct S { uint value; ~this() { } } const S a = S(12); S b = a; } test.d(10): Error: cannot implicitly convert expression a of type const(S) to S The reason is that the context pointer stored in a is const, and thus implies that the context it points to is also const. This cannot be copied to the non-const context pointer in b. This context pointer is not, however, actually treated as const. The following code compiles and passes: unittest { int i = 0; struct S { int n; void fun() const { i++; } } const S s; assert(i == 0); s.fun(); assert(i == 1); } Full discussion thread at https://forum.dlang.org/thread/p7lp2b$1jod$1@digitalmars.com I would argue that the correct solution is to allow the assignment.The text was updated successfully, but these errors were encountered: