Object Oriented Programming In Fsharp

Even though F# is a functional language, it also provides way to build application in a object oriented way. When interacting with C# libraries, it is very useful since C# is object oriented by nature. Today I would like to show you how you can define and build classes in a object oriented way.

Classes

Abstract classes / interfaces

Inheritance

1. Classes

Defining classes is the same as defining record type, by using the keyword type.
Classes can have constructors and members which can either be a function or a property.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

type MyType(name) =
let mutable name = name
do
() // do some side effect
member self.DoSomething() =
()
member self.PropName
with get() = name
and set value =
name <- value
new() =
new MyType("default name")
let mytype = new MyType()

There is a primary constructor defined on the class name type MyType(name).
And members can either be a function like DoSomething or a property with a getter and setter like PropName.
All side effects must be performed in a do statement before the members.
Extra constructors can be defined after the members using the new syntax.
Lastely any settable properties can be set directly from the empty constructor.

2. Abstract classes / interfaces

Interfaces are defined the same way as classes with type with abstract members.

type MyType(name) =
let mutable name = name
interface IMyInterface with
member self.DoSomething() = ()
member self.PropName
with get() = name
and set value =
name <- value
new() =
new MyType("default name")

interface ... with is used to implement interfaces. To implement multiple interfaces, repeat the interface ... with notation.

This way of implementing interfaces is very useful when interacting with C# libraries for functions which requires arguments implementing certain interfaces.

Conclusion

This was a quick look at how to use F# in a OOP way. We saw how to define classes, abstract classes, interfaces and how to implement those.
F# also provides a neat way to implement interfaces in expression.
I hope you enjoyed reading this post as much as I enjoyed writing it.
If you have any comments, leave it here or hit me on Twitter @Kimserey_Lam.
See you next time!