Friday, September 20, 2013

The 4 types of F#, Class, Discriminated Union, Record, and Struct

Being used to object-oriented programming in C# and other imperative languages the concept of a Class and a Struct is very familiar. I’m doing some work with F# at the moment and noticed that F# have in addition to Classes and Struct two other types, namely the Discriminated Union and Record.

From the view-point of F# development I thought it would be useful to write down when and how to use each of these four types. The decision on when to use one type instead of the others lies with the developer and before I go further with my work I want to have simple set of rules in the back of my head. It may feel like a waste of time in many cases to argue for one type over the other, but that is exactly the point. How the type is intended to be used should dictate choice of the type and when you know what you need the type for can you decide if it is a class, record etc. The decision should be natural and intuitively while implementing something and it can only be that if the mental rules are fixed and well remembered.

So here it its. A when to use, when not to use the fundamental types of F#

No comments:

Post a Comment