English 中文(简体)
F# Riddle: how to call an overload of a method?
原标题:

First part: call F# from F#

Let s say we have the following type defined in F#:

type MyClass =
    static member Overload1 (x, y) = "Pim"
    static member Overload1 (x : System.Tuple<_, _>) = "Pam"
    static member Overload1 x = "Pum"

You are now in another module (in another file).

How can you call each of the three methods shown above?

Second part: call C# from F#

Now, you define a class in C#:

public class MyClass {
    public static string Overload1<a, b>(a x, b y) { return "Pim"; }
    public static string Overload1<a, b>(Tuple<a, b> x) { return "Pam"; }
    public static string Overload1<a>(a x) { return "Pum"; }
}

From a F# code, how can you call each of the three methods now defined in C#?

最佳回答

Here s an answer to the F# part:

MyClass.Overload1(1,2)
MyClass.Overload1<_,_>(unbox (box (1,2)) : System.Tuple<int,int>)
MyClass.Overload1 1
问题回答

Hmm, I am unclear if it is possible to call the F# "Pam" method. But here s the rest.

C#:

using System;
namespace CSharp
{
    public class MyClass
    {
        public static string Overload1<a, b>(a x, b y) { return "Pim"; }
        public static string Overload1<a, b>(Tuple<a, b> x) { return "Pam"; }
        public static string Overload1<a>(a x) { return "Pum"; }
    }
}

F#:

namespace FSharp

type MyClass =
    static member Overload1 (x, y) = "Pim"
    static member Overload1 (x : System.Tuple<_, _>) = "Pam"
    static member Overload1 x = "Pum"

namespace DoIt

module Examples =
    let CallFSharp() =
        printfn "%s" <| FSharp.MyClass.Overload1(1,2)   // Pim
        printfn "%s" <| FSharp.MyClass.Overload1((1,2)) // Pum!
        printfn "%s" <| FSharp.MyClass.Overload1(())    // Pum


    let CallCSharp() =
        printfn "%s" <| CSharp.MyClass.Overload1(1,2)             // Pim
        printfn "%s" <| CSharp.MyClass.Overload1<int,int>((1,2))  // Pam
        printfn "%s" <| CSharp.MyClass.Overload1(())              // Pum

    do
        CallFSharp()        
        CallCSharp()        

Of course, in practice it will be rare to see methods in IL that take System.Tuple<...> objects as parameters.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签