Over a million developers have joined DZone.

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

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


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.

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


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.



Published at DZone with permission of Mark Needham, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}