www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 3731] New: Immutable class may be changed when inherits from mutable parent

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3731

           Summary: Immutable class may be changed when inherits from
                    mutable parent
           Product: D
           Version: 2.039
          Platform: x86
        OS/Version: Windows
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: tomeksowi gmail.com


--- Comment #0 from Tomasz Sowiński <tomeksowi gmail.com> 2010-01-20 12:44:48
PST ---
This merrily compiles:

class Zmienna {
    int a;
    this(int a) {
        this.a = a;
    }
}

immutable class Stala : Zmienna {
    this(int a) {
        super(a);
    }
}

void main() {
    auto st = new Stala(5);
    Zmienna zm = st;
    zm.a = 666;
//  st.a = 666; // fails
    assert (st.a == 666);
}


The above shows that an immutable class shouldn't be allowed to derive from
mutable classes. Unfortunately it won't work because Object is not (and can't
be) immutable. Not sure how to bite this one, perhaps make an opt out that says
mutable parents with no fields are OK? Or an exception only for Object?

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jan 20 2010
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3731


Steven Schveighoffer <schveiguy yahoo.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |schveiguy yahoo.com


--- Comment #1 from Steven Schveighoffer <schveiguy yahoo.com> 2010-01-21
05:43:28 PST ---
The solution would be to make it illegal to have a mutable class reference to
the base class.

In your example, this line should be an error:

Zmienna zm = st; // error, must use immutable(Zmienna) or const(Zmienna)

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jan 21 2010
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3731



--- Comment #2 from Tomasz Sowiński <tomeksowi gmail.com> 2010-01-21 11:03:00
PST ---
(In reply to comment #1)
 The solution would be to make it illegal to have a mutable class reference to
 the base class.
 
 In your example, this line should be an error:
 
 Zmienna zm = st; // error, must use immutable(Zmienna) or const(Zmienna)

Thanks, that makes sense. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Jan 21 2010