liveBook · Manning

Value type

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

A value type is a type whose variables directly contain their data and whose values are copied when assigned or passed by value.

A value type stores its data directly, passes values by value, and copies the value when it moves between bindings or function calls. In F#, value types commonly appear through struct tuples and records marked with the [<Struct>] attribute. section 5.2.2 section 5.3.3

The choice is practical rather than automatically superior. Reference tuples incur garbage-collection costs, while value tuples incur copying costs, especially when they contain many fields. Prefer the ordinary reference representation unless measured performance evidence justifies the extra struct syntax. section 5.2.2

F# applies value-type syntax to two common data shapes:

  • Use struct tuples when a tuple should have value semantics instead of the default reference semantics. section 5.2.2
  • Add [<Struct>] to a record when the record itself should become a value type; benchmark before changing an established model. section 5.3.3

A struct tuple

let y = 1, 2 let x = struct (1, 2) The first expression creates an ordinary F# tuple; the second explicitly creates a value tuple. Struct annotations also affect how the tuple is deconstructed and annotated. section 11.1.4 section 5.2.2

let y = 1,2             #1
let x = struct (1,2)    #2

Compare ordinary tuple construction with the explicit struct form. section 11.1.4

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

How deep it goes
Explains storage, passing, copying, and garbage-collection tradeoffs, but defers detailed performance measurement to testing.
Examples
Uses struct tuple syntax and [<Struct>] record syntax as concrete examples.
What you can do after
Apply F# struct syntax to create value tuples or value-type records, then benchmark the resulting representation.
How it connects
Connects value types to F# records, tuples, reference types, garbage collection, C#, and .NET interoperability.

Also explained in

Compare how 2 books explain value type →

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.