-
Notifications
You must be signed in to change notification settings - Fork 4.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Extend using directives to support unbound generics #116
Comments
@leppie That would be a partially-bound generic directive. I'm concerned that pushing for such a feature as part of this request would further delay fixing what I see as a major oversight in the specification of using alias directives. The only thing I'm requesting is the ability to reference unbound generic types. 📝 My original request does mention partially-bound types, but only for the sake of suggesting that my proposed syntax is least likely to interfere with the design of such a feature in the future. |
I realised I made a mistake, on that could not be aliased at all:
Trying to alias
It would be nice though to define equivalent types like that without using inheritance. I have no idea how though ;p |
I would prefer to include the generic type parameter in the alias. I would also like to see the alias support partially bound generics. Recursion could be an issue, but to shut that down I would say that aliases cannot refer to themselves (or perhaps other aliases). using StringDictionary<T> = Dictionary<String, T>; |
This has multiple downsides.
This is a separate feature request. While related to this one, I think it should be filed as a separate issue. |
You cannot bring this up without having these specific features mentioned. If aliases are to be enhanced to support generics in any form I see no reason why these specific capabilities would be segregated. That is an incredibly arbitrary delineation. http://roslyn.codeplex.com/discussions/575542 |
This is still more than zero characters per argument, especially considering you can use zero characters and always match names with the original definition.
The only case where /// <seealso cref="Dictionary<TKey, TValue>"/> In all other cases, 📝 The preferred syntax for referencing generic types in documentation comments is actually the following: /// <seealso cref="Dictionary{TKey, TValue}"/> |
And: public class StringDictionary<TValue> : Dictionary<String, TValue> { } People are already used to writing stuff like this. And again, that, right there, is of the most common* use cases that comes up when mentioning adding generic support to aliases. * Anecdotal given conversations on these forums plus stackoverflow. |
In this example, This is actually an important distinction. Consider the following: using Alias<,> = System.Collections.Generic.Dictionary<,>;
// in a method, this assertion should succeed:
Assert.AreSame(typeof(Alias<,>), typeof(System.Collections.Generic.Dictionary<,>)); |
Why does it need to? Is that only because "unbound" is in the title of this proposal? I get that you're trying to solve the simple case and avoid the complications. My concern is that solving this in this manner now does complicate solving it for partially bound in the future as you would probably end up with multiple syntaxes. As for those complications, I've confirmed that C# already doesn't permit an alias to reference another alias, including itself. Keep that rule in place and the major issues regarding generic type parameters disappear. using L1 = System.Collections.Generics.List<L>; // illegal
using I32 = System.Int32;
using L2 = System.Collections.Generics.List<I32>; // also illegal |
Per your request I have opened a separate proposal. Feel free to beat it up. I fully recognize that this would involve more effort and likely has more complications than I've enumerated but I'd rather follow the path and see where it leads. Shake as many issues out into the open. Maybe it's not as bad as we think. |
I prefer the syntax proposed in #3993 |
Closing in favor of #3993. |
I propose extending the Using alias directives feature of C# as follows.
The generic-dimension-specifier and unbound-type-name productions are defined in the C# Language Specification description of the
typeof
operator.The semantics of an unbound using alias directive are almost identical to the semantics of using alias directives as we know them; they merely provide the ability to associate a generic arity to the alias. The following additional requirements would be in effect:
Since this proposal is intended to solve a specific limitation in the support for
using
directives (for which there is no real workaround), it is written in the simplest form possible. It neither provides nor prevents a future expansion of the language from supporting partially-bound type parameters in a using alias directive; I believe this is a problem for another time.Example
C# currently allows the following using statements:
This expansion would additionally allow the following:
One might argue that the duplication of
<>
on the left and right sides would be unnecessary. I believe the syntax should appear this way for the following reasons:With the exception of
var
, C# consistently requires types to specify generic type arguments or generic arity when referencing types. Using inference in this specific case would be misleading, especially for the following:Arity inference could cause future challenges to a language extension designed to support partially-bound type parameters in a using alias directive.
The text was updated successfully, but these errors were encountered: