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 7019077
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:08:10+00:00 2026-05-27T23:08:10+00:00

Below a Compose function. If f and g are unary functions which return values,

  • 0

Below a Compose function. If f and g are unary functions which return values, then Compose(f,g) returns a function which when called on x performs the equivalent to f(g(x)).

static Func<X, Z> Compose<Z, Y, X>(Func<Y, Z> f,Func<X, Y> g) 
{ return x => f(g(x)); }

Here’s a couple of simple Func values which can be composed:

Func<int, bool> is_zero = x => { return x == 0; };

Func<int, int> mod_by_2 = x => { return x % 2; };

E.g. this works:

Console.WriteLine(Compose(is_zero, mod_by_2)(4));

However, if I instead have these equivalent static methods:

static bool IsZero(int n) { return n == 0; }

static int ModBy2(int n) { return n % 2; }

the same example doesn’t work with those. I.e. this produces a compile time error:

Console.WriteLine(Compose(IsZero, ModBy2)(4));

Explicitly passing types to Compose fixes the issue:

Console.WriteLine(Compose<bool, int, int>(IsZero, ModBy2)(4));

Is there anyway to write Compose such that it works on the static methods without the explicit types?

Is this a good approach to take to implementing Compose? Can anyone make improvements to this?

  • 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-05-27T23:08:11+00:00Added an answer on May 27, 2026 at 11:08 pm

    The problem here is not the use of static methods but the use of method groups. When you use a function name as an expression without invoking it then it’s a method group and must go through method group conversion. You would have the exact same problem with instance methods.

    The problem you’re running into is that C# can’t do return type inference on method groups. Using Compose(IsZero, ModBy2)) requires the return type to be inferred for both IsZero and ModBy2 and hence this operation fails.

    This is a known limitation in the inference capabilities of the C# compiler. Eric Lippert wrote an extensive blog article on this particular subject which covers this problem in detail

    • http://blogs.msdn.com/b/ericlippert/archive/2007/11/05/c-3-0-return-type-inference-does-not-work-on-member-groups.aspx
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Below is my current char* to hex string function. I wrote it as an
Below is part of the XML which I am processing with PHP's XSLTProcessor :
Below is my $.ajax call, how do I put a selects (multiple) selected values
I'm curious if it's possible to take several conditional functions and create one function
Regarding the SMS compose view as show in the picture below: alt text http://www.kennethlund.dk/wordpress/wp-content/uploads/2008/08/iphone_sms.jpg
Consider the code below (which has been simplified). I have a service class that
I am using this code below to swap on image on hover: $(function() {
Below I have a very simple example of what I'm trying to do. I
Below are two ways of reading in the commandline parameters. The first is the
Below is the code of a simple html with a table layout. In FF

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.