Better where clauses type constraints #4654
Unanswered
seblaf1
asked this question in
Language Ideas
Replies: 1 comment
-
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I believe the current where clauses have limitations and they could easily be much more powerful and help us better with beautiful code designs.
I suggest two things:
The first case is pretty straight forward. We could simply add parameterized new clauses which would allow developers to create instances of a class without specifying provider functions, as well as offering a way to ensure a generic type isn't abstract without having to provide a default constructor.
In the second case, it is better shown using an example. I have class
StateHandler<TState, TResult> where TState : State<TResult> where TResult : class
, andState<TResult> where T Result : class
. The problem is I would like to use TResult in my StateHandler type definition without having to explicitly tell what my TResult is, rather infer it from the generic of my TState, similarly than how generic methods work. It would also allow me to not specifying constraints on TResult twice.Currently, if you have a method like so:
MyMethod<T>(T instance)
, you can invoke that method without specifying T:MyMethod(myObject)
. I would like to be able to specify my StateHandler type by simply specifying TState:StateHandler<MyStateType>
, but still being able to use TResult in StateHandler. A solution would be allowing a syntax similar to this:StateHandler<TState> where TState : State<TResult>
, where not specifying TResult gives us access to the generic type instead of imposing an additional constraint on TState.Beta Was this translation helpful? Give feedback.
All reactions