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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:05:01+00:00 2026-05-16T23:05:01+00:00

I know virtually nothing about F#. I don’t even know the syntax, so I

  • 0

I know virtually nothing about F#. I don’t even know the syntax, so I can’t give examples.

It was mentioned in a comment thread that F# can declare functions that can take parameters of multiple possible types, for example a string or an integer. This would be similar to method overloads in C#:

public void Method(string str) { /* ... */ }
public void Method(int integer) { /* ... */ }

However, in CIL you cannot declare a delegate of this form. Each delegate must have a single, specific list of parameter types. Since functions in F# are first-class citizens, however, it would seem that you should be able to pass such a function around, and the only way to compile that into CIL is to use delegates.

So how does F# compile this into CIL?

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

    When you’re writing C# and you need a function that can take multiple different parameter sets, you just create method overloads:

    string f(int x)
    {
        return "int " + x;
    }
    string f(string x)
    {
        return "string " + x;
    }
    void callF()
    {
        Console.WriteLine(f(12));
        Console.WriteLine(f("12"));
    }
    // there's no way to write a function like this:
    void call(Func<int|string, string> func)
    {
        Console.WriteLine(func(12));
        Console.WriteLine(func("12"));
    }
    

    The callF function is trivial, but my made-up syntax for the call function doesn’t work.

    When you’re writing F# and you need a function that can take multiple different parameter sets, you create a discriminated union that can contain all the different parameter sets and you make a single function that takes that union:

    type Either = Int of int
                | String of string
    let f = function Int x -> "int " + string x
                   | String x -> "string " + x
    
    let callF =
        printfn "%s" (f (Int 12))
        printfn "%s" (f (String "12"))
    
    let call func =
        printfn "%s" (func (Int 12))
        printfn "%s" (func (String "12"))
    

    Being a single function, f can be used like any other value, so in F# we can write callF and call f, and both do the same thing.

    So how does F# implement the Either type I created above? Essentially like this:

    public abstract class Either
    {
        public class Int : Test.Either
        {
            internal readonly int item;
            internal Int(int item);
            public int Item { get; }
        }
        public class String : Test.Either
        {
            internal readonly string item;
            internal String(string item);
            public string Item { get; }
        }
    }
    

    The signature of the call function is:

    public static void call(FSharpFunc<Either, string> f);
    

    And f looks something like this:

    public static string f(Either _arg1)
    {
        if (_arg1 is Either.Int)
            return "int " + ((Either.Int)_arg1).Item;
        return "string " + ((Either.String)_arg1).Item;
    }
    

    Of course you could implement the same Either type in C# (duh!), but it’s not idiomatic, which is why it wasn’t the obvious answer to the previous question.

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

Sidebar

Related Questions

I know that calling a virtual method from a base class constructor can be
I know that I can share files using Shared Folders in Virtual PC, but
I know nothing about Load Test Software. Is Visual Studio Load Test Virtual User
I know it is a good practice to declare virtual destructors for base classes
Know of an OCAML/CAML IDE? Especially one that runs on Linux?
We all know what virtual functions are in C++, but how are they implemented
I want to know what a virtual base class is and what it means.
I know python functions are virtual by default. Let's say I have this: class
Does anyone know of a way to paste over a visually selected area without
I want to know, how sockets are implemented in the Java Virtual Machine. Is

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.