liveBook · Manning

Tuple

As explained in F# in Action: A Revised Edition of Get Programming with F#

A tuple is a fixed-size, ordered data structure that combines values, potentially of different types, into a single composite value.

A tuple groups several values into one lightweight F# value. It is useful when values belong together briefly: returning more than one result, passing several inputs together, or exploring a small piece of data without defining a dedicated type. Tuples are product types: they combine values with “and,” such as a name and an address. section 5.2 section 5.1

Create and deconstruct a tuple

Construct a tuple with comma-separated values, then deconstruct it by binding one name per position. The elements have no declared field names, so the names used on deconstruction communicate their intended meaning. section 5.2.1 section 5.2.1

let name = "isaac", "abraham"       #1
let firstName, secondName = name    #2

This short example shows a two-string tuple being bound to one symbol and then split into first and second names. section 5.2.1

A tuple type lists element types in order with `*`: a pair of strings is `string * string`, while a four-part value might be `string * string * int * string`. Tuples can have any length, and F# infers their structure in bindings and function signatures. A function can accept a tuple, discard an unneeded element with `_`, and return another tuple. When inferred generality is too loose for the intended domain, add a type annotation. section 5.2.2 section 5.2.2 section 5.2.2

Return a parsing result

Many .NET `TryParse` methods become ordinary F# tuple results. Bind the success flag and parsed value together, then use the flag before consuming the value. This avoids manually handling C# out parameters. section 5.2.2 section 11.1.5

Use tuples deliberately rather than as a default domain model:

  • Choose them for simple, short-lived groups of two or three values, especially within a function or during exploration. section 5.2.3
  • Prefer a record for longer-lived or public data whose fields need formal names, dot access, or easier updates. section 5.2.3 section 5.5
  • Avoid ambiguous combinations such as `string * string` unless the surrounding symbol or function name makes the positions obvious. section 5.2.3
  • Expect deconstruction to be the only field-access style; changing one element requires destructuring and rebuilding the tuple. section 5.2.3 section 5.5

Tuples support immutability and structural equality, but their positional design becomes harder to read as they grow. Nested tuples are possible, as are tuples containing records, yet deeper shapes can make signatures and deconstruction cumbersome. For performance-sensitive interop, F# can use a value-type tuple with `struct`; ordinary F# tuples are the default, while `struct` tuples map to `System.ValueTuple` for C# consumers. section 5.5 section 5.2.2 section 11.2.1

Tuples also fit function design because every F# function has one input and one output. Package multiple arguments or results as a tuple when they should travel together; use curried functions instead when callers should supply arguments one at a time. Keep data separate from behavior by modeling a shape with tuples, records, or unions and placing operations in modules. section 6.4 section 10.1.2

How F# in Action: A Revised Edition of Get Programming with F# covers it

How deep it goes
Explains construction, deconstruction, signatures, inference, wildcards, nesting, struct tuples, and trade-offs; defers complex modeling to records.
Examples
Uses name pairs, name-and-age values, a doctor-title function, nested tuples, and `TryParse` results.
What it assumes
Builds from primitive and .NET values, introducing tuple composition without requiring prior F# or .NET type-modeling knowledge.
What you can do after
Apply it to create, deconstruct, annotate, and selectively discard tuple values while choosing records for richer models.
How it connects
Connects tuples to product types, records, anonymous records, curried functions, type inference, immutability, and C# interop.

Also explained in

Compare how 15 books explain tuple →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of F# in Action: A Revised Edition of Get Programming with F#. The section links point to the parts of the book it draws on.