[Issue 3944] New: Require immutable annotation for new fields of class inherited from immutable class

d-bugmail at puremagic.com d-bugmail at puremagic.com
Fri Mar 12 14:57:56 PST 2010


http://d.puremagic.com/issues/show_bug.cgi?id=3944

           Summary: Require immutable annotation for new fields of class
                    inherited from immutable class
           Product: D
           Version: 2.041
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: nobody at puremagic.com
        ReportedBy: bearophile_hugs at eml.cc


--- Comment #0 from bearophile_hugs at eml.cc 2010-03-12 14:57:54 PST ---
This is a wrong D2 program:


immutable class Foo {}
class Bar : Foo { int x; }
void main() {
    auto b = new Bar;
    b.x = 10; // line 5
}


>From the error message it's clear that x is not a mutable variable:
test.d(5): Error: can only initialize const member x inside constructor

But from the code of the Foo class there is no clear indication that x is
immutable. So in this situation I think it's better if the compiler requires a
immutable annotation on x too, for code readability, something like:


immutable class Foo {}
class Bar : Foo { int x; } // Error: attribute x requires 'immutable'
annotation
void main() {
    auto b = new Bar;
}



immutable class Foo {}
class Bar : Foo { immutable int x; } // OK, no error
void main() {
    auto b = new Bar;
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list