Declaring a type as a Tuple in F#
March 26, 2017 Leave a comment
We saw how Tuples work in F# in this post. Tuples are containers for various elements such as this one:
let myFirstTuple = (45, "hello world", 5., true, addThreeNumbers)
If you’d like to declare a type to be of Tuple then you can use the ‘*’ character as follows:
type Address = string * string * int type House = { colour: string; address: Address } let myHome = {colour = "blue"; address = ("Milano", "Via Napoletana", 32)}
The “address” type is a Tuple that consists of two strings and an integer. The House type has an address field of type Address. When we then declare a House type we provide the ingredients of the Tuple field in the same way as in the first example, i.e. in a comma-delimited list of values. They will be substituted into the individual elements of the Tuple.
View all F# related articles here.