Replies: 4 comments
-
Generally, I think adding the ability to omit generic parameters that can be inferred would be a great feature. However, I don't like the idea of having to explicitly tell the compiler that the type can be implied via an class Factory<T,TX> where T : IParametrizedClass<TX> { ...
var x = Factory<MyParametrizedClass,>... |
Beta Was this translation helpful? Give feedback.
-
I agree, |
Beta Was this translation helpful? Give feedback.
-
We could also consider specifying that parameter is implicit in generic declaration: class Factory<T, implicit TX> where T : IParameterizedClass<TX>
{
}
...
var x = new Factory<MyParametrizedClass>(); //compiler uses generic class with correct inferred type This way we avoid hanging commas in declaration, so that syntax looks much nicer. Maybe this could even be extended to use cases like this: class MyList<implicit T>
{
public MyList(IEnumerable<T> items)
{
...
}
}
...
void MyMethod(IEnumerable<int> items)
{
var myList = new MyList(items); //type MyList<int>
} |
Beta Was this translation helpful? Give feedback.
-
When I made this proposal, I had same idea as @ghord , but I decided rather be explicit about generics parameters. Generally I think, this can lead to "ghosts" during refactoring - if you look at code, you can't decide if it is generics class or not, you can't even tell which of generics variants it is. Example
There are many problems in this example
Maybe it will work in simplier examples and in advanced scenarios you can't use it. But with omitting types, it will still work
Ommiting generics parameters in type declaration is generic question. Maybe it shouldn't be allowed at all.
|
Beta Was this translation helpful? Give feedback.
-
Implicit generics parameters capture
Summary
[summary]: Allow derive parameters using where condition
class Factory<T,TX> where T : IParametrizedClass<TX>
var x=Factory<MyParametrizedClass,>
Motivation
[motivation]: If you need to construct class and work with its generics parameters.
Example
Detailed design
Type can be pretty safely inferred except example
in that case compiler will fail.
But developer can still use full syntax
Factory<MyParametrizedClass, string>
Drawbacks
Why should we not do this?
Alternatives
Unresolved questions
What parts of the design are still undecided?
Design meetings
Link to design notes that affect this proposal, and describe in one sentence for each what changes they led to.
Beta Was this translation helpful? Give feedback.
All reactions