Implicit generic parameter declarations #9084
Replies: 3 comments 9 replies
-
@JimmyCushnie I don't think the rules you laid out are enough to make things work or whether it's even possible to remove the parameters without additional syntax.. so you probably want something like this: class ClassB<TClassWithGenerics...>
where TClassWithGenerics : ClassWithGenerics<...> // or ClassWithGenerics<TClassWithGenerics...>
{
// ...
} This should be lowered by the compiler to the following form: class ClassB<TClassWithGenerics, TItems, TDataChunk, TItemsComparer>
where TClassWithGenerics : ClassWithGenerics<TItems, TDataChunk, TItemsComparer>
where TItemsComparer : IEqualityComparer<TItems>
{
// ...
} I think that this makes things more clear and easier to explain so you might want to update the OP. |
Beta Was this translation helpful? Give feedback.
-
So, if I mistype the name (or the type gets removed in a library upgrade) of a type that is used as generic type argument in a constraint, the signatures/arity of types/methods are changed and I get unrelated/confusing errors at the use-sites of the type/method instead of its declaration? Sounds like a bad idea UX-wise... |
Beta Was this translation helpful? Give feedback.
-
Hello. I've been a C# developer for 8 years, but this is my first time proposing a new language feature. Please let me know if I'm doing anything wrong; I couldn't find any guidelines on how to write productive proposals for this project.
Background
When writing code with a lot of generic parameters, you end up with a lot of repetitive boilerplate. For example:
The relevant base class,
ClassWithGenerics
, specifies three generic parameters, one of which has a type constraint. This is fine and makes sense.However, every place we want to work with
ClassWithGenerics
, we have to repeat the generic parameters as well as the constraint. This is bad and annoying: it clutters the codebase and makes refactoring needlessly more difficult.Proposal
To solve the described problem, I propose the following language feature:
With these rules, the above example becomes the following:
Much tidier, easier to read, and easier to maintain.
I'm currently working on a generics-heavy codebase and this would make my life much easier.
What do you think?
Beta Was this translation helpful? Give feedback.
All reactions