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);
}
}
Comment #1 by razvan.nitu1305 — 2018-03-29T11:07:05Z
The real problem is that delegating constructor calls are not allowed after labels, and the case and default statements are labels. The compiler should detect those.
The trouble with labels is the primitive flow analysis done by the compiler cannot handle them. So they are just disallowed. Maybe a future enhancement could cover it.
Comment #4 by robert.schadek — 2024-12-13T18:58:01Z