Type alias on right side of using alias directive

145 views Asked by At

I wanted to do

namespace n
{
    using TupT = Tuple<TextBox, string, Func<bool>, string>;

    // Doesn't compile
    // using DicT = Dictionary<Button, TupT>;

    // Have to repeat tuple definition: cumbersome and error prone
    using DicT = Dictionary<Button, Tuple<TextBox, Func<bool>, string>>;
    // ...
}

This is forbidden by par. 9.3.1 of this language spec: http://msdn.microsoft.com/en-us/library/aa664765%28v=vs.71%29.aspx. Generally, type aliases are not visible to other using alias directives. (Generics are actually not an issue, but the demand for aliasing occurs frequently with generics because the type denotations can become complex.)

Why is that so?

Edit: Yes, I know: I can do it differently in a myriad ways. But I'm specifically wondering why I cannot do it this way ;-)

1

There are 1 answers

3
Timothy Shields On

I suppose this restriction is simply to rule out having to worry about situations like the following in the compiler.

using X = C<Y>;
using Y = C<X>;
class C<T> { }

Of course the problem is that X is C<Y> is C<C<X>> is C<C<C<Y>>> is ...

If the language restriction was looser, disallowing only circular using alias directives, the compiler could feasibly handle that. I think this is just a feature with very low priority - the C# language team only has so much time to design and implement features.

If you find yourself utilizing using alias directives very often, it's an indicator that your common using alias directive should really be made into a class. I'm sure Tuple<TextBox, string, Func<bool>, string> isn't nearly as descriptive as a tiny four-property model class.