digitalmars.D.learn - One path skips constructor - is this a bug?
- Piotr Mitana (34/34) Sep 07 2017 Code:
- Jesse Phillips (10/20) Sep 07 2017 This seems likely a bug, my understanding of this error is for
- Johan Engelen (2/4) Sep 07 2017 http://www.digitalmars.com/d/archives/digitalmars/D/learn/Throwing_excep...
Code: =================================== import std.conv; import std.regex; struct A { int field1; int field2; this(int field1, int field2) { if(field1 > field2) throw new Exception("This is illegal!"); } this(string str) { if(str.match(ctRegex!(`^[0-9]{4}-[0-9]{2}`))) this(str[0..4].to!int, str[5..7].to!int); else throw new Exception("Invalid string"); } } void main() { A(2004, 43); A("2004-43"); } =================================== This throws a compilation error: main.d(17): Error: one path skips constructor main.d(15): Error: return without calling constructor Why do I need the constructor call, if I throw the exception anyway? Is this a bug?
Sep 07 2017
On Thursday, 7 September 2017 at 16:08:53 UTC, Piotr Mitana wrote:Code: =================================== import std.conv; import std.regex; struct AThis throws a compilation error: main.d(17): Error: one path skips constructor main.d(15): Error: return without calling constructor Why do I need the constructor call, if I throw the exception anyway? Is this a bug?This seems likely a bug, my understanding of this error is for class inheritance, where it must call the super constructor for all code paths to create the base class. With a struct the compiler always has the means to create the base and so it just seems to be be saying, "hey you called the constructor maybe you still want to do that here." And it would be a nice improvement to have control flow, but you may get away with it if you add assert(0); to the end of the constructor.
Sep 07 2017
On Thursday, 7 September 2017 at 16:08:53 UTC, Piotr Mitana wrote:main.d(17): Error: one path skips constructor main.d(15): Error: return without calling constructorhttp://www.digitalmars.com/d/archives/digitalmars/D/learn/Throwing_exception_in_constructor_28995.html
Sep 07 2017