Let's say I have an abstract Node class and BinaryOperation class derived from Node and three classes IntegerConstant, RealConstant and CharacterConstant also derived from Node.
The general design for the BinaryOperation class would look like this:
public class BinaryOperation : Node
{
public Node Operand1 { get; private set; }
public Node Operand2 { get; private set; }
//...
}
I also have parsing methods for those classes, each of them returning given type. So IsIntegerConstant() is of type IntegerConstant and so on.
Now when parsing an operand in expression I could easily check the type like this:
Node operand1 = IsIntegerConstant() ??
IsRealConstant() ??
IsCharacterConstant();
However C# does not allow that. Because compiler cannot determine the type of the expression in operand1 assignment. What should I do? Should I make all of the parsing methods return type Node or cast each method to Node. Or maybe create separate BinaryOperation class members for each of the operand types?
Nodefor the type inference to work? Annoying, but better than changing your design substantially to make the syntax a little cleaner.