1

An idea from tacit programming is to not apply arguments to functions if it can be avoided.

Why doesn't F# allow this to compile if functions are first class members?

type IAdder =
    interface
    abstract member Add : int -> int -> int
end

type Adder =
    interface IAdder with
        member this.Add x y = x + y

type AdderWithInnerAdder(adder:IAdder) =
    interface IAdder with
        member this.Add = adder.Add

I get the compilation error...

No abstract property was found that corresponds to this override

I feel that this should compile. adder.Add clearly implements IAdder.Add and should be acceptable.

t3dodson
  • 3,949
  • 2
  • 29
  • 40

1 Answers1

2

You can't assign interface members as if they were functions. Interfaces don't work like that. You have to specify the parameters:

    member this.Add x y = adder.Add x y

But interfaces are generally bleh. They're only good for passing generic functions without losing genericity. When functions are non-generic, interfaces are strictly inferior.

If you were willing to go with a more functional approach instead, life would get easy fast:

type Adder = { Add: int -> int -> int }
let adder() = { Add = fun x y -> x + y }
let adderWithInnerAdder adder = { Add = adder.Add }
Fyodor Soikin
  • 78,590
  • 9
  • 125
  • 172
  • 1
    I like that there is a more functional way to approach this. The constraint is I have to interop with code written in C#. Thanks for your answer. – t3dodson Mar 03 '17 at 05:27
  • 2
    @t3dodson If you need to interop with C# then curried args should probably be avoided in your interfaces too. Working with `FSharpFunc`s in C# code is not going to make for an idiomatic experience. – TheInnerLight Mar 03 '17 at 12:10
  • 1
    @TheInnerLight, They only look curried from F# side. Member functions and let-bound functions are compiled to normal methods regardless of currying. – Fyodor Soikin Mar 03 '17 at 13:14
  • @FyodorSoikin what do you mean "let-bound function" can you link documentation about how this works on the C# side? – t3dodson Mar 04 '17 at 03:37
  • Let-bound functions are functions defined with `let`, for example `let f x y = x + y`. I don't have a link to any documentation on how stuff is compiled. You can easily check yourself with ILSpy. – Fyodor Soikin Mar 04 '17 at 04:52