One path skips constructor - is this a bug?

Piotr Mitana via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Sep 7 09:08:53 PDT 2017


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?


More information about the Digitalmars-d-learn mailing list