Is it possible to define a general extension method in F #?

I tried using Rhino Mocks with F # code, and the following code was problematic:

let service = MockRepository.GenerateMock<IMyService>()
service.Stub(s => s.Name).Return("Service");

This was not a surprise since Stub is not part of the IMyService interface, it is a C # extension method that defines Rhino Mocks.

Too much changed code works:

let service = MockRepository.GenerateMock<IMyService>()
RhinoMocksExtensions.Stub<IMyService, string>(service, fun s -> s.Name).Return("Service");

However, it would be nice to define an extension method in F #, but then it will be a parameterized generalized extension method, which will take a tuple. I tried varios syntax, but with no luck. I did not find information on whether this is supported in F # or not. If anyone knows, please let me know.

+5
source share
2 answers

, . 8.12.1 (Imported # Extensions Members) :

# F # # - . ,

  • #, "this" F # F #

  • #, "this" - . F # F #

, # (, System.Linq), , , .

, F # , . , F # (, IEnumerable<string>), .

+3

kvb, F # 2.0. Rhino.Mocks, F #, :

let mstub f target =
    RhinoMocksExtensions.Stub(target, Function(f))

let mreturn value (options: IMethodOptions<'a>) =
    options.Return value

let service = MockRepository.GenerateMock<IComparer>()
service |> mstub (fun s -> s.Compare(1,2)) |> mreturn 1 |> ignore

. ( , , )

+4

All Articles