· f

F#: Object expressions

One of the things I miss a bit from the Java world is the ability to create anonymous inner classes which implement a certain interface.

We can’t do this in C# - you always need to define a named class - but in my latest playing around with F# I was quite pleased to learn that we do have this ability using a feature called object expressions.

These come in particularly useful when you are only making use of the implementation of an interface in one place in the code and therefore don’t want to expose this type to any other code.

Given an interface:


type ICarRepository =
    abstract FindAll : Unit -> list<Car>

type Car() =

The normal way to implement that interface if we are following the C# approach would be like this:


type CarRepository() =
	interface ICarRepository with     
	    member self.FindAll () = [ new Car() ] 

We would then reference the CarRepository in our code:


let myCarRepository = new CarRepository();;

The alternative is to create a value which implements the interface inline, a way of coding which is easier to implement instead of having to create a new type each time we want to implement an interface.


let myCarRepository = 
{ new ICarRepository with 
     member self.FindAll () = [ new Car() ] }

This seems to be quite in keeping with the style of programming being taught in the Real World Functional Programming book which seems to be around getting code working quickly and then working iteratively towards a more reusable solution.

I’d be interested to see how well this technique would work on a real project since I get the feeling that quite a lot of interface implementations are only used in one place and therefore don’t really need to have a type defined purely for implementing them - that decision can be delayed until one is actually needed.

  • LinkedIn
  • Tumblr
  • Reddit
  • Google+
  • Pinterest
  • Pocket