F#: Using C# Extension Methods

An interesting thing I noticed about referencing C# libraries from F# is that you can't access C# extension methods in the same way that you would be able to if you were using the library from C# code.

I came across this problem when playing around with the Rhino Mocks framework in some F# code.

I wrote a simple test to see whether I could get an expectation to work correctly, without paying any regard for the fact that you can't use C# extension methods in the same way as you can from C# code!

open Xunit
open Rhino.Mocks

type IFoo =
abstract Bar : string -> string

type Foo() =
interface IFoo with
member x.Bar (value) = value

type Baz(foo: IFoo) =
member x.Barry(value) = foo.Bar(value) |> ignore

[<Fact>]
let my_mocking_test () =
let foo = MockRepository.GenerateMock<IFoo>()

foo.Expect(fun f -> f.Bar("random")).Return("someValue")

let baz = new Baz(foo)
baz.Barry("random") |> ignore

foo.VerifyAllExpectations();

That code doesn't compile with lines 18 and 23 being the offending ones.

When you think about it I suppose it's not really that surprising since C# extension methods are actually just syntactic sugar (added in C# 3.0) and what really happens at compile time is that a static method is created for each of our extension methods, taking in the type for which the extension method is defined as an argument.

If we want to use them in our F# code we therefore need to call them specifically. With a little exploration through Rhino Mocks I came up with the following code.

...
[<Fact>]
let my_mocking_test () =
let foo = MockRepository.GenerateMock<IFoo>()
(RhinoMocksExtensions.Expect<IFoo, string>(foo, fun f -> f.Bar("random"))).Return("someValue") |> ignore

let baz = new Baz(foo)
baz.Barry("random") |> ignore

RhinoMocksExtensions.VerifyAllExpectations(foo)

It doesn't read particularly fluently although it does work. I imagine an equivalent extension method could be written in F# to get around the problem although I ended up not needing to do any mocking after I first wrote this code so I haven't looked into how to do that yet.

 

References
0

Mark Needham is a software developer and consultant at ThoughtWorks. I have a keen interest in developer testing and object oriented design of systems. Mark is a DZone MVB and is not an employee of DZone and has posted 21 posts at DZone.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)