I'm struggling to wrap my brain around type inference within an asynchronous extension method I've adopted from this answer.
public static class EnumerableExtensions
{
public static async Task<IEnumerable<T1>> SelectAsync<T, T1>(
this IEnumerable<T> enumeration,
Func<T, Task<T1>> func)
{
return await Task.WhenAll(enumeration.Select(func));
}
public static async Task<IEnumerable<T1>> SelectManyAsync<T, T1>(
this IEnumerable<T> enumeration,
Func<T, Task<IEnumerable<T1>>> func)
{
return (await enumeration.SelectAsync(func)).SelectMany(x => x);
}
}
I'm making use of the extension methods as follows:
var allChildren = await parents.SelectManyAsync(parent => ReturnsChildrenAsync(parent));
parents
is of type IList<Parent>
ReturnsChildrenAsync()
returns a Task<IList<Child>>
But I'm getting the following error:
The type arguments for method 'EnumerableExtensions.SelectManyAsync<T, T1>(IEnumerable<T>, Func<T, Task<IEnumerable<T1>>>)' cannot be inferred from the usage. Try specifying the type arguments explicitly.
It seems that T1
cannot be inferred. Why can't this be derived from Task<IList<Child>>
?