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?
When you’re writing C# and you need a function that can take multiple different parameter sets, you just create method overloads:
The
callFfunction is trivial, but my made-up syntax for thecallfunction 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:
Being a single function,
fcan be used like any other value, so in F# we can writecallFandcall f, and both do the same thing.So how does F# implement the
Eithertype I created above? Essentially like this:The signature of the
callfunction is:And
flooks something like this:Of course you could implement the same
Eithertype in C# (duh!), but it’s not idiomatic, which is why it wasn’t the obvious answer to the previous question.