-
Notifications
You must be signed in to change notification settings - Fork 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
[Umbrella] Generics Improvements #253
Comments
Related to #151 p.s. The first part of the post reminds me TypeScript's generic defaults. :) |
I'm the original poster of #169 ( I'm not entirely clear on the state of the CLR and how feasible requests requiring changes are right now on a short timeframe, but I would prefer to see this implemented as a CLR constraint rather than a C# only one. Does anyone know if they'd consider it a no-go breaking change to implement a feature like this as C# only and then move it to a CLR restriction at a later time? I think that would mostly be fine. (I wasn't entirely sure how on-topic this paragraph could be considered, so I've gone ahead and started a separate discussion about the status of the CLR at #255.) I also have a few other generics requests lounging about in my head, so if his has a chance to grow into a big discussion on generics I may try to get those posted soon. |
Please also consider to merge this proposal to the umbrella #193 |
Please also consider to cover by the umbrella this proposal #266 . |
Partially inferred generic arguments: TResult GetValue<TKey, TResult>(TKey key) { ... }
var result = GetValue<int>("abc"); alternatively var result = GetValue<, int>("abc"); Partially resolved generic aliases: using StringDictionary<T> = Dictionary<string, T>; |
@GeirGrusom For the first example you could use named type arguments. For generic aliases see #90. |
@alrz Ah yes, I missed that part. |
#268 proposing |
Umbrella topics/proposals like this make it difficult for the individual ideas under the umbrella to be considered by the LDM. |
Right. I'll try to separate these out into multiple issues. |
@alrz Did you split them up yet? If so, please link them here. |
@alrz , did you create an issue for "Inference from the first usage"? |
@ymassad No, I didn't think that would worth it. If you think it does, feel free to open an issue for it. |
default type parameters
Proposal: dotnet/roslyn#6248
Since types with identical names can be defined with different arities, it might be easier to require the diamond syntax (or missing type arguments) to disambiguate type arities. But not requiring it would provide source-compat such that you can add a type parameter with a default value, without changing use sites.
One possible restriction to simplify binding rules is to disallow identical type names where there exists a type with default type parameters.
However, even with this restriction there can be ambiguous type names, in those cases we prefer the non-generic type.
If we want to select the default parameter we can use
X<>
syntax or named type arguments.We can also disallow leading default type parameters just like method default parameters,
this
as default type parameterIn some cases it's useful to define a default type parameter of the inheriting type (if any).
It'd be nice to also constrain such types to the inheriting type, in case that it's explicitly specified. (#169)
But this would be a C#-only restriction as CLR does not support such constraints. Note that this constraint only applies to the derived types so it might be preferable to restrict it to interfaces and abstract classes.
Named type arguments
Proposal: dotnet/roslyn#6207
Named type arguments are useful for various use cases:
Again, just like named arguments, named type arguments do not need to be in the same order that they are declared, as long as all non-default type parameters are specified.
It will be possible to mix positional and named type arguments,
Inline type parameter constraints
Proposal: dotnet/roslyn#7671
With numerous type parameters, type constraints require duplicating every type parameter to the point that it's somehow confusing,
Inlining type parameters could significantly reduce noise around the type declaration,
This syntax would bring type parameter constraints closer to their declaration,
Inlined type constraints can be mixed with type parameters, e.g.
Multiple type constraints can be inlined using "intersection types", using whatever syntax that they're going to use,
However, it might be actually preferable to only allow "simple" type constraints in this manner.
Inferred type arguments
Even if a type parameter does not have a default, the compiler can infer it in some circumstances,
As for binding, we will prefer non-generic types just like default type parameters described above. So if there exist multiple type with the same name it wouldn't be possible to use type inference in the use-site.
The text was updated successfully, but these errors were encountered: