Type arguments cannot be inferred: why?
The following C # code cannot be output.
using System.Collections.Generic;
namespace Test
{
public class Program
{
public static void Main()
{
Pipeline<string[]> s = null;
// Compiles fine.
s.WithNewDelimiters<Pipeline<string[]>, string[], string>();
// Fails to compile.
s.WithNewDelimiters();
}
}
public class Pipeline<T> { }
public static class Extensions
{
public static void WithNewDelimiters<TPipeline, TEnumerable, TElement>
(this TPipeline pipeline)
where TPipeline : Pipeline<TEnumerable>
where TEnumerable : IEnumerable<TElement>
{
}
}
}
Although the reason this is a generic extension method is irrelevant, so I can link C-ers like this in any instance Pipeline<T>
Now, as an aside, here's my logic: I read from left to right, so this is not the algorithmic way the C # compiler works.
- Let's consider first
TPipeline
.TPipeline
isPipeline<TEnumerable>
We passed inPipeline<string[]>
. It means thatTEnumerable == string[]
- Then browse
TEnumerable
.TEnumerable
is anIEnumerable<string>
- Since it
TElement
has no restrictions, it can be anything, so you can install it onstring
.
Obviously this logic is either flawed or doesn't work with the C # compiler. I think I was wrong in assuming that the value of one constraint affects the other.
But I'm still not sure why this is ambiguous for the C # compiler. There is no other type parameter configuration that could satisfy the requirements here, right?
+3
source to share
No one has answered this question yet
See similar questions:
or similar: