This question is more about good programming practises and design decisions, but it is centred around the switch
in the constructor.
Idea is to handle default behaviour in variety of ways. Default constructor (Const()
) handles the simplest or most common case, and parametrized constructor (Const(int type)
) uses switch
to enumerate other specific cases.
To save some code I could define default constructor in, say case: 0
, then I could refer to it from the basic constructor like so:Const() { this(0); }
(and I could not do it otherwise (i.e. refer to basic constructor from switch case) because compiler demands one and only one reference to another constructor as first statement).
But I would like to also be able to handle incorrect input in the parametrized constructor with sending default:
case to case: 0
(maybe in combination with error message like “Invalid case. Using default case (0) instead.”) or vice versa to make it more robust.
I searched and read other questions here (see this and that, for instance), and I could see where it could be done with some sort of twisted switch fall-through scheme, but for the sake of semantics and clarity I would like to keep the default case first, or, on the other hand, is it possible to refer to default statement via the constructor (such as this.Const(default)
or the sort)? Can it be done at all, and if so, what are philosophically best approaches to this problem?