www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 19928] New: disallow modification of immutable in constructor

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

          Issue ID: 19928
           Summary: disallow modification of immutable in constructor
                    after calling base ctor
           Product: D
           Version: D2
          Hardware: x86
                OS: Mac OS X
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: schveiguy yahoo.com

This breaks immutability:

import std.stdio;
class C
{
    void foo() { writeln("C");}
    this()
    {
        foo();
    }
}

class D : C
{
    immutable int x;
    this()
    {
        super();
        x = 5;
        foo();
    }
    override void foo()
    {
        writeln("D: ", x);
    }
}

void main()
{
    new D;
}

x could be considered final once any base ctor is called, or really any
function which accepts a path back to x (like if you call some external
bar(this), it should be the same thing).

--
May 31 2019