DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Building Scalable Real-Time Apps with AstraDB and Vaadin
Register Now

Trending

  • What ChatGPT Needs Is Context
  • Implementing a Serverless DevOps Pipeline With AWS Lambda and CodePipeline
  • How To Check IP Addresses for Known Threats and Tor Exit Node Servers in Java
  • 4 Expert Tips for High Availability and Disaster Recovery of Your Cloud Deployment

Trending

  • What ChatGPT Needs Is Context
  • Implementing a Serverless DevOps Pipeline With AWS Lambda and CodePipeline
  • How To Check IP Addresses for Known Threats and Tor Exit Node Servers in Java
  • 4 Expert Tips for High Availability and Disaster Recovery of Your Cloud Deployment
  1. DZone
  2. Coding
  3. Frameworks
  4. F#: Using C# Extension Methods

F#: Using C# Extension Methods

Mark Needham user avatar by
Mark Needham
·
Jun. 15, 09 · News
Like (0)
Save
Tweet
Share
3.63K Views

Join the DZone community and get the full member experience.

Join For Free

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.

 

Extension method Library Framework Testing IT

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

Opinions expressed by DZone contributors are their own.

Trending

  • What ChatGPT Needs Is Context
  • Implementing a Serverless DevOps Pipeline With AWS Lambda and CodePipeline
  • How To Check IP Addresses for Known Threats and Tor Exit Node Servers in Java
  • 4 Expert Tips for High Availability and Disaster Recovery of Your Cloud Deployment

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: