H
Harold Howe
I am running into a situation where the compiler complains that it
cannot infer the type parameters of a generic method when one of the
function arguments is an anonymous method.
Here is a complete code example:
//-----------------------------
using System;
using System.Collections.Generic;
interface ForwardIterator<T> {}
class IterImpl<T> : ForwardIterator<T> {}
static class Algorithm
{
// method that takes a delegate as its last argument
public static void ReplaceIf<T>(ForwardIterator<T> begin,
ForwardIterator<T> end,
Predicate<T> func)
{}
// another method that takes a delegate
public static I RemoveIf<I,T>(I begin, I end, Predicate<T> func)
where I: class, ForwardIterator<T>
{
return null;
}
}
class Program
{
static void Main(string[] args)
{
IterImpl<int> begin = new IterImpl<int>();
IterImpl<int> end = new IterImpl<int>();
// This compiles fine
Algorithm.ReplaceIf(begin, end, delegate(int x){return true;});
// This also compiles
Predicate<int> pred = delegate(int x){return true;};
Algorithm.RemoveIf(begin, end, pred);
// This errors out
Algorithm.RemoveIf(begin, end, delegate(int x){return true;});
}
}
//-----------------------------
The error is
main.cs(38,9): error CS0411: The type arguments for method
'Algorithm.RemoveIf<I,T>(I, I, System.Predicate<T>)' cannot be
inferred from the usage. Try specifying the type arguments explicitly.
I can get it to compile by passing template arguments, but that is
hideous. Why can't the compiler infer the second usage when it can infer
the first? Am I missing something simple?
H^2
cannot infer the type parameters of a generic method when one of the
function arguments is an anonymous method.
Here is a complete code example:
//-----------------------------
using System;
using System.Collections.Generic;
interface ForwardIterator<T> {}
class IterImpl<T> : ForwardIterator<T> {}
static class Algorithm
{
// method that takes a delegate as its last argument
public static void ReplaceIf<T>(ForwardIterator<T> begin,
ForwardIterator<T> end,
Predicate<T> func)
{}
// another method that takes a delegate
public static I RemoveIf<I,T>(I begin, I end, Predicate<T> func)
where I: class, ForwardIterator<T>
{
return null;
}
}
class Program
{
static void Main(string[] args)
{
IterImpl<int> begin = new IterImpl<int>();
IterImpl<int> end = new IterImpl<int>();
// This compiles fine
Algorithm.ReplaceIf(begin, end, delegate(int x){return true;});
// This also compiles
Predicate<int> pred = delegate(int x){return true;};
Algorithm.RemoveIf(begin, end, pred);
// This errors out
Algorithm.RemoveIf(begin, end, delegate(int x){return true;});
}
}
//-----------------------------
The error is
main.cs(38,9): error CS0411: The type arguments for method
'Algorithm.RemoveIf<I,T>(I, I, System.Predicate<T>)' cannot be
inferred from the usage. Try specifying the type arguments explicitly.
I can get it to compile by passing template arguments, but that is
hideous. Why can't the compiler infer the second usage when it can infer
the first? Am I missing something simple?
H^2