Declaring a type as a Tuple in F#

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.

Advertisement

About Andras Nemes
I'm a .NET/Java developer living and working in Stockholm, Sweden.

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 )

Facebook photo

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

Connecting to %s

Elliot Balynn's Blog

A directory of wonderful thoughts

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog

WEB APPLICATION DEVELOPMENT TUTORIALS WITH OPEN-SOURCE PROJECTS

Once Upon a Camayoc

Bite-size insight on Cyber Security for the not too technical.

%d bloggers like this: