liveBook · Manning

Option

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

An option is an F# generic discriminated union representing either the presence of a value with Some or its absence with None.

An option makes missing data explicit. In F#, a value of type 'T option is either Some value or None, rather than a nullable version of 'T. Because it is a generic discriminated union, it can wrap integers, strings, records, and other values. section 9.1.2

let presentNumber : int option = Some 10   #1
let missingNumber : int option = None      #2
let mandatoryNumber : int = 10

Compare a present integer, a missing integer, and a mandatory integer. The option annotation records absence as part of the type. section 9.1.2

Optionality changes how code consumes a value. An option cannot be used as though it were the contained value: pattern match to handle both Some and None. Avoid .Value in application code; it can throw when the value is None and bypasses the safety the type provides. section 9.1.2

Transform optional data without boilerplate

When a customer might not exist, keep the uncertainty while transforming the customer. Option.map applies a normal function only for Some and returns None unchanged, so ordinary functions such as getAge and classifyAge can be piped over optional data. Chain maps, or compose the functions first, instead of repeatedly unwrapping and rewrapping values. section 9.1.3

Use options to model absence deliberately:

  • Use pattern matching, or Option.defaultValue, at the point where the None case must receive a final response. section 9.2.6
  • Use Option.map or Option.bind to continue working with Some data while preserving the option container. section 9.2.6
  • Prefer List.tryFind and related try functions when a collection lookup may fail; they return an option rather than throw. section 9.1.6
  • Use List.choose when a function produces options for collection items and only present values should remain. section 9.1.6

Keep Option inside an F# domain and convert at .NET boundaries. Option.ofObj and Option.ofNullable safely bring null references and Nullable values into option form; toObj and toNullable convert outward. For mandatory reference data, reject null at the boundary rather than repeatedly checking it in pure F# code. section 9.1.5

Option represents an expected absence, not every kind of failure. Use Result when callers need information about why an operation failed, and reserve exceptions for genuinely exceptional, fail-fast situations. A wrapper can also adapt a .NET TryParse-style API into Some for success and None for failure. section 9.2.6 section 11.1.7

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

How deep it goes
Defines the discriminated-union cases, safe pattern matching, mapping pipelines, collection operations, and .NET conversion boundaries.
Examples
Uses optional integers, database-loaded customers and ages, collection searches, null conversion, and a TryParse wrapper.
What it assumes
Builds on discriminated unions while introducing Option as a generic wrapper for mandatory or missing values.
What you can do after
Apply it via Option.map, pattern matching, collection try functions, and .NET conversion helpers to represent missing data safely.
How it connects
Connects Option to discriminated unions, pattern matching, Option.map, Option.bind, Result, exceptions, and .NET Nullable values.

Also explained in

Compare how 9 books explain option →

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.