Jul 8, 2011

f# essential - part 14 - object oriented without class

JavaScript is duck type language, if you expect a object has a method, you just call object.method. It supports Polymorphism very well, but you don't need implementation inheritance, and you don't need prototype, constructor at all. F#'s record type has similar concept. It is not a class, but it is strongly type, and it is super easy to create, it is like javascript object in strongly type. I think it is the one of best features in F#. Its syntax is as following

type YourRecordType = 
    { member1: type1;
      member2: type2 }

      member x.function1() = 

Why it is so interested to me. So first, the definition of record type is similar a interface. So it is strongly type. However, the implementation is not defined. More importantly, it is very easy to create an object, which is the implementation. For example

let yourObject = {
      member1: m1;
      member2: m2;

Woo, that is so simple, if you make a member mutable, you can even switch implementation on the fly.