Implementing a C# interface with an F# type

Suppose you have the following C# interface ICalculator with 4 functions:

namespace Project.Domains
{
	public interface ICalculator
	{
		int Add(int x, int y);
		int Subtract(int x, int y);
		float Divide(float x, float y);
		int Multiply(int x, int y);
	}
}

We can implement this interface in an F# type as follows:

module CSharpExamples

open Project.Domains

type FSharpCalculator() =
    interface ICalculator with
        member this.Add(x, y) = x + y
        member this.Subtract(x, y) = x - y
        member this.Divide(x, y) = x / y
        member this.Multiply(x, y) = x * y

Interfaces must be implemented explicitly in F#. We declare each implementing member with the ‘member’ keyword. If any of the interface functions are missing then we get a compile time error similar to the following:

No implementation was given for ‘ICalculator.Multiply(x: int, y: int) : int’. Note that all interface members must be implemented and listed under an appropriate ‘interface’ declaration, e.g. ‘interface … with member …’.

Using the FSharpCalculator class is a bit strange at first though. At least I expected the following code to work just fine:

let calcImpl = new CSharpExamples.FSharpCalculator()
int res = calcImpl.Add(10, 20)

However, that results in an error:

The field, constructor or member ‘Add’ is not defined.

It turns out that we have to cast the implementation to the interface before we call its functions using the :> operator:

let calcImpl = new CSharpExamples.FSharpCalculator()
let calcAbs = calcImpl :> ICalculator
let sum = calcAbs.Add(10, 20);

Now it works as expected.

View all F# related articles here.

Advertisements

About Andras Nemes
I'm a .NET/Java developer living and working in Stockholm, Sweden.

One Response to Implementing a C# interface with an F# type

  1. Pingback: F# Weekly #20, 2017 – Join FableConf in Bordeaux! – Sergey Tihon's Blog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

ultimatemindsettoday

A great WordPress.com site

Elliot Balynn's Blog

A directory of wonderful thoughts

Robin Sedlaczek's Blog

Developer on Microsoft Technologies

Softwarearchitektur in der Praxis

Wissenswertes zu Webentwicklung, Domain-Driven Design und Microservices

Technology Talks

on Microsoft technologies, Web, Android and others

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog

WEB APPLICATION DEVELOPMENT BEST PRACTICES WITH MICROSOFT STACK & ANGULAR

Cyber Matters

Bite-size insight on Cyber Security for the not too technical.

Guru N Guns's

OneSolution To dOTnET.

Johnny Zraiby

Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

%d bloggers like this: