This project is read-only.

Allow specifying only some types of parameters of a lambda

Topics: C# Language Design
Apr 10, 2014 at 12:54 PM
I've encountered this before, and a question on SO reminded me of this issue today:

Sometimes, you have a lambda, where one parameter can be inferred and another can't. Consider a simple (and mostly nonsensical) example:
class PartialEval<T>
    private readonly T value;

    public PartialEval(T value)
        this.value = value;

    public Func<T2, TResult> Eval<T2, TResult>(Func<T, T2, TResult> func)
        return value2 => func(value, value2);
If you try to call it like this:
var partialEval = new PartialEval<string>("foo");
var result = partialEval.Eval((s, e) => s.EndsWith(e));
you get an error that the type arguments for Eval() cannot be inferred.

The following two versions work:
var result = partialEval.Eval((string s, string e) => s.EndsWith(e));
var result = partialEval.Eval<string, bool>((s, e) => s.EndsWith(e));
But both require you to specify one type more than necessary. What I would like is if the following code also compiled (since the type of the first parameter can be inferred):
var result = partialEval.Eval((s, string e) => s.EndsWith(e));
What do you think?
Apr 13, 2014 at 12:05 PM
I’ve run into that too...