-
Notifications
You must be signed in to change notification settings - Fork 4k
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
Proposal: Allow otherwise unresolvable simple names to resolve to a generic type of the same name with inferred type parameters #8214
Comments
This seems to be overcomplicated, just to avoid writing a non-generic class in order to surface a cleaner API. I don't like the idea of relocating the generic type arguments around. What about nested generic types? The generic type arguments can be the same name in that case (it is a compiler warning). Seems like a recipe for odd generic incantations of mixing names, commas and semicolons. It's also important to note that non-generic types and generic types of each arity are all distinctly different types according to the CLR and to C#. You mention |
This is actually #2319 with a relaxed context. I'd be happy to restrict this only to what F# offers to keep it simple (hence the title) but it seems without relocation it's a lot less useful. For example, without this, even #952 doesn't offer anything to avoid mentioning the parent class and you'd end up with
You can use semicolons to specify nested generic type parameters inward, for example
It woudn't be a problem I guess, because semicolon syntax is still positional, however, it'd be a problem for #6207 which in that case you will need to specify all type parameters in turn.
I doubt if you have that deep nested generic types, unless you code like #4494. For simple cases it woudn't be that much confusing and rather expressive, e.g.
I'm exactly addressing this fact, for example, in Java there is no such problem and you're free to use
That works for a single non-generic or generic type. If there was more then we're back to the using System.Option<>;
using System.Action<,>; However, for |
Closing in favor of #1470. |
I want to propose a name resolution for generic types similar to F#'s, plus a relocation extension which I will discuss further.
Usually a separate non-generic class is solely exists to provide static factory methods for a generic type. With this feature there would be no need for that anymore and we can use the same class to define static factory methods and use it while we take advantage of type inference.
Consider this class:
To clear up when we choose a generic type with a simple name, here's the procedure:
A
exists in the scope we choose that. OK.A
exist in the scope we need diamond syntax to disambiguate. Fail.A
exists in the scope we choose that. OK.Note: Using diamond syntax to disambiguate generic types is discussed over at #2319.
This is current syntax:
And proposed one:
Relocation means that we specify the type's type arguments in method type arguments location, delimited with a semicolon. When we go down to nested types, we will also use semicolon to separate each type's type arguments.
This is specifically useful together with #952, #6739 and type invocation (#206):
This feature can be used with others like #6207 and #5429:
One other use case is in
using
directives. Currently we can define type aliases for any non-generic type likeusing Callback = System.Action;
and with #3993 also for genericsusing Cache<T> = Dictionary<string, T>;
. But we can't useusing
for concrete types like:And if we could, there was no way to express generic types. Java doesn't have this problem because of type erasure.
So assuming
Option<T>
type above we can write:To simply bring that single type into the scope. Obviously, if there was a non-generic type named
Option
either we can keep it or disambiguate it with diamond syntax.Insignificant type parameters: If the type has no default generic type parameter (#6248) we can take advantage of this feature in methods e.g.
Related: #1470
The text was updated successfully, but these errors were encountered: