Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8539697
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:25:41+00:00 2026-06-11T11:25:41+00:00

I was playing around with a hobby project when I came across a type-inference

  • 0

I was playing around with a hobby project when I came across a type-inference error I didn’t understand. I have simplified it to the following trivial example.

I have the following classes and functions:

class Foo { }
class Bar { }
class Baz { }

static T2 F<T1, T2>(Func<T1, T2> f) { return default(T2); }
static T3 G<T1, T2, T3>(Func<T1, Func<T2, T3>> f) { return default(T3); }

Now consider the following examples:

// 1. F with explicit type arguments - Fine
F<Foo, Bar>(x => new Bar());

// 2. F with implicit type arguments - Also fine, compiler infers <Foo, Bar>
F((Foo x) => new Bar());

// 3. G with explicit type arguments - Still fine...
G<Foo, Bar, Baz>(x => y => new Baz());

// 4. G with implicit type arguments - Bang!
// Compiler error: Type arguments cannot be inferred from usage
G((Foo x) => (Bar y) => new Baz());

The last example produces a compiler error, but it seems to me that it should be able to infer the type arguments without any problems.

QUESTION: Why can’t the compiler infer <Foo, Bar, Baz> in this case?

UPDATE: I have discovered that simply wrapping the second lambda in an identity function will cause the compiler to infer all the types correctly:

static Func<T1, T2> I<T1, T2>(Func<T1, T2> f) { return f; }

// Infers G<Foo, Bar, Baz> and I<Bar, Baz>
G((Foo x) => I((Bar y) => new Baz()));

Why can it do all the individual steps perfectly, but not the whole inference at once? Is there some subtlety in the order that the compiler analyses implicit lambda types and implicit generic types?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T11:25:42+00:00Added an answer on June 11, 2026 at 11:25 am

    Because the algorithm as described in the C# specification doesn’t succeed in this case. Let’s look at the specification in order to see why this is.

    The algorithm description is long and complicated, so I’ll heavily abbreviate this.

    The relevant types mentioned in the algorithm have the following values for you:

    • Eᵢ = the anonymous lambda (Foo x) => (Bar y) => new Baz()
    • Tᵢ = the parameter type (Func<T1, Func<T2, T3>>)
    • Xᵢ = the three generic type parameters (T1, T2, T3)

    Firstly, there’s the first phase, which in your case does only one thing:

    7.5.2.1 The first phase

    For each of the method arguments Eᵢ (in your case, there’s only one, the lambda):

    • If Eᵢ is an anonymous function [it is], an explicit parameter type inference (§7.5.2.7) is made from Eᵢ to Tᵢ
    • Otherwise, [not relevant]
    • Otherwise, [not relevant]
    • Otherwise, no inference is made for this argument.

    I’ll skip the details of the explicit parameter type inference here; it suffices to say that for the call G((Foo x) => (Bar y) => new Baz()), it infers that T1 = Foo.

    Then comes the second phase, which is effectively a loop that tries to narrow down the type of each generic type parameter until it either finds all of them or gives up. The one important bullet point is the last one:

    7.5.2.2 The second phase

    The second phase proceeds as follows:

    • […]
    • Otherwise, for all arguments Eᵢ with corresponding parameter type Tᵢ where the output types (§7.5.2.4) contain unfixed type variables Xj but the input types (§7.5.2.3) do not, an output type inference (§7.5.2.6) is made from Eᵢ to Tᵢ. Then the second phase is repeated.

    [Translated and applied to your case, this means:

    • Otherwise, if the return type of the delegate (i.e. Func<T2,T3>) contains an as yet undetermined type variable (it does) but its parameter types (i.e. T1) do not (they do not, we already know that T1 = Foo), an output type inference (§7.5.2.6) is made.]

    The output type inference now proceeds as follows; again, only one bullet point is relevant, this time it’s the first one:

    7.5.2.6 Output type inferences

    An output type inference is made from an expression E to a type T in the following way:

    • If E is an anonymous function [it is] with inferred return type U (§7.5.2.12) and T is a delegate type or expression tree type with return type Tb, then a lower-bound inference (§7.5.2.9) is made from U to Tb.
    • Otherwise, [rest snipped]

    The “inferred return type” U is the anonymous lambda (Bar y) => new Baz() and Tb is Func<T2,T3>. Cue lower-bound inference.

    I don’t think I need to quote the entire lower-bound inference algorithm now (it’s long); it is enough to say that it doesn’t mention anonymous functions. It takes care of inheritance relationships, interface implementations, array covariance, interface and delegate co-/contravariance, … but not lambdas. Therefore, its last bullet point applies:

    • Otherwise, no inferences are made.

    Then we come back to the second phase, which gives up because no inferences have been made for T2 and T3.

    Moral of the story: the type inference algorithm is not recursive with lambdas. It can only infer types from the parameter and return types of the outer lambda, not lambdas nested inside of it. Only lower-bound inference is recursive (so that it can take nested generic constructions like List<Tuple<List<T1>, T2>> apart) but neither output type inferences (§7.5.2.6) nor explicit parameter type inferences (§7.5.2.7) are recursive and are never applied to inner lambdas.

    Addendum

    When you add a call to that identify function I:

    • G((Foo x) => I((Bar y) => new Baz()));

    then type inference is first applied to the call to I, which results in I’s return type being inferred as Func<Bar, Baz>. Then the “inferred return type” U of the outer lambda is the delegate type Func<Bar, Baz> and Tb is Func<T2, T3>. Thus lower-bound inference will succeed because it will be faced with two explicit delegate types (Func<Bar, Baz> and Func<T2, T3>) but no anonymous functions/lambdas. This is why the identify function makes it succeed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Whilst playing around with resources in my visual studio 10 project, I came across
I've been playing around with Simple.Data and have run across something that I can't
Playing around with type-classes I came up with the seemingly innocent class Pair p
Playing around in order to learn XSLT, I have the following XML file and
So playing around with the expander control and I came across this code to
While playing around with one-to-one associations in castle activerecord I stumbled upon the following
I have been playing around with various methods of making the Visitor pattern in
I'm just playing around with some code but it seemed to have the specific
Playing around with Google Maps these days, with some directions. I have a map
Just playing around with interfaces and I have a question about something which I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.