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.

A value may be missing for an ordinary reason: a customer ID might not exist, or a search might find no match. F# represents that possibility with `option<'T>`, a generic discriminated union with two cases: `Some value` and `None`. An `int option` is a different type from an `int`, so a function cannot silently treat a possibly missing number as a mandatory one. section 9.1.2 section 9.1.3

Handle both outcomes

`let presentNumber : int option = Some 10` and `let missingNumber : int option = None` have the same type. To produce a description, match both cases: ```fsharp let describe number = match number with | Some value -> $"The number is {value}" | None -> "There is no number" ``` Pattern matching makes the missing-value decision explicit. Avoid using an option’s `.Value` member in application code: it throws if the value is `None`. section 9.1.2

When the next operation only transforms a present value, repeating a match adds boilerplate. `Option.map` unwraps `Some`, applies a function, and wraps its result in `Some`; `None` stays `None`. For an optional customer, `optionalCustomer |> Option.map (fun customer -> customer.Age)` produces an optional age. The age-reading function need not know about options, and mapping can be chained with further transformations. section 9.1.3

Choose the operation that matches what missing data means:

  • Use a match or `Option.defaultValue` when you need to decide what `None` means; use `Option.map` or `Option.bind` while continuing to work with optional data. section 9.2.6
  • Prefer `List.tryFind` to `List.find` when no match is expected sometimes: it returns an option rather than throwing. Use `choose` to keep the present results from a collection of optional results. section 9.1.6
  • Use `Result` when failure needs an explanation, and reserve exceptions for situations where there is no useful recovery path. section 9.2.6
let parseOption parser (value:string) =                   #1
    match parser value with
    | true, v -> Some v
    | false, _ -> None
 
let parseIntOption = parseOption System.Int32.TryParse    #2
let maybeANumber = parseIntOption "123"                   #3

A small wrapper turns a .NET `TryParse` outcome into `Some` on success or `None` on failure, so callers work with an option. section 11.1.7

At .NET boundaries, reference values can be null and value types can use `Nullable<'T>`. Convert them with `Option.ofObj` or `Option.ofNullable`, keep options inside F# domain code, and convert back when calling APIs that require .NET representations. An option should describe possible absence—not serve as a substitute for validating a mandatory value. section 9.1.5

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

How deep it goes
Defines both union cases, works through matching and mapping, and gives boundary guidance without detailing option implementation internals.
Examples
Uses present and missing integers, an optional customer’s age, collection searches, and a TryParse wrapper.
What it assumes
Introduces the two cases with integers, then uses functions and pattern matching to transform optional values.
What you can do after
Apply it via F# option functions to handle missing values explicitly and convert nullable data at .NET boundaries.
How it connects
Connects discriminated unions and pattern matching to collections, Result, exceptions, and .NET nullable values.

Also explained in

Compare how 10 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.