Sugarpink AI
Sugarpink AI

Reputation: 21

Tuple in F# Record

I am currently in the process of learning F# (only started a few days ago). And I ran into a problem that I can not find an answer to when Googling.

How can I define a member or field in a record as a tuple type?

I would have imagined it would work something like this:

type Rectangle = 
{ Coordinates : (int,int)
  Dimensions : (int,int) }

But this leads to a compile error (it works if I split the tuples into separate fields).

So how do I define a field that holds a tuple of a given structure?

Thanks for the help in advance! :)

Upvotes: 1

Views: 152

Answers (1)

Koenig Lear
Koenig Lear

Reputation: 2436

You use wildcards to define tuples:

type Rectangle = 
     { Coordinates : int*int
       Dimensions  : int*int }

This notation is based on the Cartesian product in mathematics

You use parenthesis for instantiating them:

let r = { Coordinates = (10,30); Dimensions = (5,5) }

Upvotes: 6

Related Questions