F# Tip Of The Week (Week of August 19, 2013)

Even though tuples, records and discriminated unions are reference types, they all have the built-in equality properties you would expect in a value type.  For example:


> let a = (1,'a');;

val a : int * char = (1, 'a')

> let b = (1,'a');;

val b : int * char = (1, 'a')

> a = b;;

val it : bool = true

That is, you get the equality behavior you would expect rather than the usual equality behavior associated with reference types.  Here’s an example of records and the built-in equality checking:


> type rec1 = {

a:int

b:string

};;

type rec1 =

{a: int;

b: string;}

> let a = {a=1;b="one"};;

val a : rec1 = {a = 1;

b = "one";}

> let b = {a=1;b="one"};;

val b : rec1 = {a = 1;

b = "one";}

> a = b;;

val it : bool = true

One response

  1. Pingback: F# Weekly #34 2013 | Sergey Tihon's Blog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: