[Issue 12777] const/immutable member function violating its const-ness - confusing error message
via Digitalmars-d-bugs
digitalmars-d-bugs at puremagic.com
Wed May 21 03:32:56 PDT 2014
https://issues.dlang.org/show_bug.cgi?id=12777
--- Comment #5 from Ivan Kazmenko <gassa at mail.ru> ---
(In reply to Andrej Mitrovic from comment #2)
> (In reply to Ivan Kazmenko from comment #0)
> > First, member "v" is not constant - the method is.
>
> No, it's not the 'method', it's the 'this' reference which is constant. And
> const is transitive in D, which means all fields are constant. The above is
> equivalent to:
>
> -----
> struct S {
> const(int) v; // as seen from 'fun'
> void fun () const {v++;}
> }
>
> struct S {
> immutable(int) v; // as seen from 'fun'
> void fun () immutable {v++;}
> }
> -----
>
> However I do agree the diagnostic could be improved a bit.
Thank you for the explanation! I see now how it makes sense from the
compiler's point of view. For the programmer however, it is convenient (and
"safe", right?) to reason as "method is constant means it can not modify its
object", and reading the current message somewhat breaks that model of
thinking.
--
More information about the Digitalmars-d-bugs
mailing list