sliekens / Txt

A text parsing framework for .NET.
MIT License
2 stars 4 forks source link

The 'Alternative' classes require that each element type is unique #2

Closed sliekens closed 9 years ago

sliekens commented 9 years ago

The Alternative<T, T2> class (and friends) define constructor overloads that take parameters of the generic types. Using the same type for T and T2 results in an ambiguous constructor overload.

sliekens commented 9 years ago

Suggested fix: define a single constructor that requires a parameter of the base type Element, and a numeric parameter that indicates the alternative that was chosen.

public Alternative(Element element, int choice, ITextContext context)
    : base(element.Data, context)
{
    switch (choice)
    {
        case 1:
            if (false == element is T1)
                throw new ArgumentException();
            break;
        case 2:
            if (false == element is T2)
                throw new ArgumentException();
            break;
        default:
            throw new ArgumentException();
    }

    this.element = element;
}