Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

F#: Implementing an interface with overloaded members

Tags:

f#

I have defined an interface in F# with an overloaded method. As per compiler request, the overload uses tupled arguments instead of curried ones:

type IInterface =
    abstract member Do : (int * string) -> unit
    abstract member Do : int -> unit

I then create a class which implements the interface:

type ImplementingClass =
    interface IInterface with
        member this.Do (i, s) = ()
        member this.Do i = ()

However, doing so yields the compiler error for the first of both methods: "This override takes a different number of arguments to the corresponding abstract member"

What am I doing wrong here?

like image 464
Mathias Weyel Avatar asked Apr 08 '15 16:04

Mathias Weyel


1 Answers

There is a subtle difference between the following two:

abstract member Do : int * string -> unit
abstract member Do : (int * string) -> unit

If you add the parentheses, you're saying that the parameter is a tuple and the compiler should produce a method taking Tuple<int, string>. Without parentheses, the method will be compiled as taking two parameters. Most of the time, this is hidden and you can ignore it - but sadly, not always.

So, you can either change your interface definition to use ordinary "two-parameter" method (this would be my preferred method - you can still call the method with tuple as an argument and looks nicer in the .NET/C# view):

type IInterface =
    abstract member Do : int * string -> unit
    abstract member Do : int -> unit

type ImplementingClass =
    interface IInterface with
        member this.Do (i, s) = ()
        member this.Do i = ()

Or you can implement the interface as it is:

type ImplementingClass =
    interface IInterface with
        member this.Do((i:int, s:string)) = ()
        member this.Do(i:int) = ()

Sadly, this is a bit ugly - you need the type annotations so that the compiler can unambiguously decide which method you're implementing.

like image 86
Tomas Petricek Avatar answered Sep 30 '22 00:09

Tomas Petricek