www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 18688] New: Constructors shouldn't have implicit super call

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

          Issue ID: 18688
           Summary: Constructors shouldn't have implicit super call if it
                    throws
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: yshuiv7 gmail.com

This example doesn't compile:

class A {
    this(int x){}
     disable this();
}
class B: A {
    this(int x) {
        super(x);
    }
    this(string b) {
        switch(b) {
            case "a":break;
            default: assert(false);
        }
        this(1);
    }
}

Possibly because the compile decides 'this(1)' is not always reachable, and
tries to implicitly call super() in that case. But if 'this(1)' is not
reachable, the constructor is guaranteed to throw, thus super call should not
be required.

Also, this program has almost the same behavior (SwitchError is thrown instead
of AssertError), but it compiles:

class A {
    this(int x){}
     disable this();
}
class B: A {
    this(int x) {
        super(x);
    }
    this(string b) {
        final switch(b) {
            case "a":break;
        }
        this(1);
    }
}

--
Mar 28 2018