Bug 1621 – cannot access string member in const method
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
x86
OS
Linux
Creation time
2007-10-28T12:34:00Z
Last change time
2015-06-09T01:14:20Z
Keywords
rejects-valid
Assigned to
bugzilla
Creator
i.kasiuk
Comments
Comment #0 by i.kasiuk — 2007-10-28T12:34:37Z
Maybe I just don't understand const and invariant properly. But I wonder why the following code doesn't compile:
class A {
string s;
const string f() { return s; }
}
The error message is:
test.d(3): Error: cannot implicitly convert expression (this.s) of type const char[] to invariant(char)[]
Why does it state that s is of type const char[]? Shouldn't it be const invariant(char)[]?
Replacing "return s;" by "return cast(string) s;" fixes the compilation error. But why do I have to cast s to its own type?
The compilation error also disappears if the method is invariant instead of const.