Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

a pitfall in argument and interface inheritance in fsharp

Tags:

f#

unit-type

Is there any real reason why KO would not work ?

  type IBase = 
      abstract member test : (unit * unit) -> unit

  type OK() = 
      interface IBase with

        member x.test ((titi,tata)) = () //OK

  type KO() = 
      interface IBase with

        member x.test (titi,tata) = () //fail : This override takes a different number of arguments to the corresponding abstract member    
like image 523
nicolas Avatar asked Dec 09 '22 07:12

nicolas


1 Answers

Because the parentheses mean something in F# (it indicates a tuple), so they're not the same.

As given, the test method is defined as a method that takes a tuple of two unit values as arguments. If you use Reflection to get the MethodInfo, the method is defined as:

Void test(System.Tuple`2[Microsoft.FSharp.Core.Unit,Microsoft.FSharp.Core.Unit])

That matches the OK method, but not the KO method.

If you redefine the interface to

type IBase = 
    abstract member test : unit * unit -> unit

Then OK doesn't compile, but KO does.

This alternative version produces a test method which takes two arguments:

Void test(Microsoft.FSharp.Core.Unit, Microsoft.FSharp.Core.Unit)
like image 177
Mark Seemann Avatar answered Mar 16 '23 17:03

Mark Seemann