liveBook · Manning

Reference type

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

A reference type is a type whose values are references to objects stored separately from the reference itself.

A reference type stores a reference to an object held separately from the reference itself. In F#, reference-type values are passed by reference, stored on the heap, and subject to garbage collection. section 5.2.2

Distinguish reference types from value types when choosing between ordinary and struct-based representations:

  • Reference tuples are F#’s default tuple representation and incur garbage-collection costs. section 5.2.2
  • Value tuples use the struct keyword, pass values by value, and generally avoid garbage collection by using stack storage. section 5.2.2
  • F# records are reference types by default, but the [<Struct>] attribute changes a record into a value type. section 5.3.3
  • Prefer the reference representation unless performance testing demonstrates that copying or garbage-collection costs justify the additional struct syntax. section 5.3.3 section 5.2.2

Changing a record’s representation

A normal record such as Address uses reference-type semantics. Adding [<Struct>] before the declaration makes it a value type instead; test the performance impact before making that change. section 5.3.3

[<Struct>]
type Address =
    {
        Line1 : string
        ...
    }

Compare the ordinary record declaration with the [<Struct>] form to see the small syntax change that selects value-type semantics. section 5.3.3

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

How deep it goes
Explains passing, heap storage, garbage collection, copying costs, and the syntax for selecting value types, without deeper runtime internals.
Examples
Uses System.Tuple, F# records, and [<Struct>] declarations to contrast reference and value representations.
What you can do after
Evaluate representation choices using allocation and copying trade-offs, then justify struct usage with performance evidence.
How it connects
Connects reference types with value types, tuples, records, heap storage, stack storage, garbage collection, and interoperability.

Also explained in

Compare how 2 books explain reference 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.