Saturday, November 19, 2011

F# ≥ C# (Record)

Continue previous post, let us continue to explore how easily F# can reduce your coding effort compare to C#.

Until recently I started to notice the record type in F#. Originally I thought it was like C#'s struct or class, but it is not true.

type myPointRecord = { X : float; Y::float }
let pointA = { X = 0; Y= 0 }
let pointB = { X = 0; Y = 0 }

Two points are actually same. The way to prove they are same are very simple and intuitive:

let isEqual = pointA = pointB

The biggest advantage for a record is you can compare its content with "=" operator. I tried to compare two C# structs with "==", but it does not work. It is option to override C#'s equals and GetHashCode function to make a class to support non-reference comparison, but that is a lot of work especially when you are trying to make all your class to support this kind of content comparison.

No comments: