Allow dynamic arguments to be used on local functions with inferred generic parameters #2051
Replies: 6 comments
-
What limitation? I can use |
Beta Was this translation helpful? Give feedback.
-
@HaloFour I think the code above has a typo and the goal here is to be able to write: public static TField Required<TField>(this TField field) where TField : Field
{
void AddTo<T>(Field<T> typedField) => typedField.AddValidator(new RequiredValidator<T>());
AddTo((dynamic)field);
return field;
}
|
Beta Was this translation helpful? Give feedback.
-
@HaloFour Yeah sorry @svick caught the typo. I'm just trying to turn the second function into a local function inside the first function. |
Beta Was this translation helpful? Give feedback.
-
I didn't realize the limitation was only applicable to local generic functions...fixed now. |
Beta Was this translation helpful? Give feedback.
-
Ah, I see. Given the very specific error message it seems quite intentional. I'd make the assumption that the team was aware that such inference was legal with non-local functions. Maybe they consider that to be a mistake? |
Beta Was this translation helpful? Give feedback.
-
@HaloFour I think the issue is more that calls to local functions with dynamic parameters don't emit any actual dynamic code but rather a static call that casts the dynamic parameters. I'm guessing it would have been extra work to make sure the proper dynamic code was emitted in the case of a generic method with inferred arguments. Local functions can't have overloads so converting it to a static call works fine as long as there aren't any inferred generic parameters. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Is the limitation against using
dynamic
on local generic functions with inferred type parameters due to a large technical hurdle or just a matter of nobody identifying compelling use cases for it yet?Being able to do that would let me keep things better contained in situations like this, which I'm running into fairly often right now to get around generic inference limitations and lack of generic wildcards:
I want to be able to do this:
This is a simple example but there are more complicated flows where this would really help keep things better organized and more linear.
Beta Was this translation helpful? Give feedback.
All reactions