Add the ability to overloadable generics that prefer more explicit types. #6408
-
Is there any way to allow the compiler to prefer more explicit overloads when a generic is overloaded. This is a very common issue when dealing with Async Extension Methods. In this simple scenario Adding an Async suffix would be the simple solution but that can quickly become problematic in more complex scenarios when building a more functional/Fluent library. I use async as that is where I run into it the most, but I have run into numerous places where this would apply. public static T ExtensionMethod<T>(this Func<T> func){
return func();
}
public async static Task<T> ExtensionMethod<T>(this Func<Task<T>> func){
return await func();
} |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 3 replies
-
@cportwood could you clarify what the issue is? The compiler will prefer the more explicit form here: As you can see, it's calling the second method, instantiated with |
Beta Was this translation helpful? Give feedback.
-
@CyrusNajmabadi Thanks, I will do some investigation. My assumption is obviously incorrect for the simple example. I will do some work after I have some time and come back with a better example. I am working with a library where chained Func<T,R> and Func<T, Task> are causing issues and I ran into a similar issue with another project. I will reopen when I come up with a valid example. Thanks for the rapid response. |
Beta Was this translation helpful? Give feedback.
-
In my experience, the problem I've faced is the other way around: when I mistakenly specify |
Beta Was this translation helpful? Give feedback.
@cportwood could you clarify what the issue is? The compiler will prefer the more explicit form here:
As you can see, it's calling the second method, instantiated with
int
, versus teh first, instantiated withTask<int>
.