www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 12777] New: const/immutable member function violating its

https://issues.dlang.org/show_bug.cgi?id=12777

          Issue ID: 12777
           Summary: const/immutable member function violating its
                    const-ness - confusing error message
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: minor
          Priority: P1
         Component: DMD
          Assignee: nobody puremagic.com
          Reporter: gassa mail.ru

If a const/immutable member function tries to modify the contents of its struct
or class, the error message is present (ok) but confusing (not ok).

Example:

-----
struct S {
    int v;
    void fun () const {v++;}
    void gun () immutable {v++;}
}
class C {
    int v;
    void fun () const {v++;}
    void gun () immutable {v++;}
}
void main () {}
-----

Error message:

-----
const_member.d(3): Error: can only initialize const member v inside constructor
const_member.d(4): Error: can only initialize const member v inside constructor
const_member.d(8): Error: can only initialize const member v inside constructor
const_member.d(9): Error: can only initialize const member v inside constructor
-----

The error is indeed there: none of these functions are allowed to alter "v". 
However, the error message text is confusing.

First, member "v" is not constant - the method is.  So, no apparent reason to
mention the constructor.

Second, altering a variable is not necessarily initialization ("v++;").

--
May 21 2014