liveBook · Manning

Missing value

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

Missing data is information that is absent, unavailable, or not provided for an expected field or value.

A missing value is data that is absent even though a field is expected. This is ordinary domain reality: an e-commerce account needs an email address but may not have a home address, perhaps because the customer buys only digital goods. The design problem is making absence understandable, consistent, and reliable rather than leaving every caller to guess. section 9.1

`null` collapses two importantly different cases: a mandatory value that was not supplied, and a genuinely optional value. In null-based designs, either case can appear in the same form, the type system may not distinguish them, and member access requires every caller to remember a check or risk a runtime null-reference error. section 9.1.1

Model absence explicitly so code must deal with it deliberately:

  • Use F# `Option` for data that is genuinely optional in the domain. Optional values force code to account for the missing case rather than treating any reference as potentially null. section 2.1.4 section 9.5
  • Keep `Option` within domain code instead of mixing it with `Nullable<T>` and naked nulls. A single representation makes optionality easier to reason about. section 9.1.5
  • Validate mandatory reference values at the application boundary. Reject null input there, then treat admitted values as non-null inside pure F# code. section 9.1.5
  • Convert at .NET boundaries: turn null references and `Nullable<T>` values into options on entry, and unwrap options only when returning to .NET-facing code. section 9.1.5

Boundary conversion

When a .NET API supplies a `string` or `Nullable<int>`, convert it immediately with `Option.ofObj` or `Option.ofNullable`. Once converted, subsequent F# code can work uniformly with options instead of repeatedly checking for null. section 9.1.5

This approach shifts work earlier: the compiler requires branches for optional data and can expose incomplete initialization. That may mean fixing more compile errors, but it avoids relying solely on tests to discover null-related runtime failures. section 2.1.4

Options address absence; `Result` adds information for an unhappy path. `Option.map` and `Option.bind` are common operations when transforming optional values, while exceptions remain a separate .NET error-handling choice. section 9.5

open System
 
let optionalName : string option = null |> Option.ofObj   #1
let optionalNameTwo = "Isaac" |> Option.ofObj             #2
let optionalAge = Nullable() |> Option.ofNullable         #3
let optionalAgeTwo = Nullable 123 |> Option.ofNullable    #4

Compare conversion of a null and a present string, then an empty and populated `Nullable` value, into one uniform option representation. section 9.1.5

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

How deep it goes
Explains null’s two meanings, compiler-enforced optionality, and conversion between options, nulls, and nullable values.
Examples
Uses an e-commerce home address and conversions of strings and Nullable integers into options.
What you can do after
Apply it via F# Option conversion functions to isolate nulls and nullable values at .NET boundaries.
How it connects
Connects missing values with Option, Nullable<T>, Result, exceptions, and null-reference errors.

Also explained in

Compare how 35 books explain missing value →

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.