jonnifyfsharp

Module C1.08

Option, Result & railway errors

No nulls, no exceptions for control flow — F# puts absence and failure in the type.

F# almost never uses null, and it does not throw exceptions for ordinary control flow. Instead, absence and failure become ordinary values, carried by two small union types: Option — Some for "a value is here", None for "it is not" — and Result — Ok for success, Error for failure with a reason attached. Because they are unions, a program reads them with match and threads them with map and bind. This module covers them in three dives — Option for what might be missing, Result for what might fail, and the railway-oriented style that wires failable steps into a pipeline that stops at the first error.

Two unions for the things that go wrong

Most languages reach for null when a value might be absent, and for an exception when an operation might fail. Both are off to the side of the type: a function returns int, and the caller cannot tell from the signature that it might also return nothing, or throw. F# folds both cases back into the return type with two unions from the C1.06 toolbox.

The first dive covers Option — the type-safe replacement for null. A function that might not produce a value returns Some x or None, and the caller must account for both before reaching the x inside. The second covers Result, which is the same shape with a reason on the failing side: Ok x or Error e. The third composes failable steps into a railway: each step returns a Result, and the first Error switches the whole computation onto the error track. Select a dive below to read its summary and the one idea it adds.

The three dives · select one

C1.08.1 Option C1.08.2 Result C1.08.3 Railway errors the value + the reason the pipeline
C1.08.1 · Option

Option<'T> is Some of 'T | None — the type-safe replacement for null. List.tryFind and Map.tryFind return it; Option.map and Option.bind transform the inside without unwrapping.

You learn to model a missing value and thread it with map and bind

Open this dive · or pick another above

Absence and failure are not special cases bolted onto the type — they are two more values a function can return, read like any other.

The three dives

Read them in order. Each stands on valid, runnable F#: every Some, None, Ok, and Error below was typed into dotnet fsi and echoes exactly what the page shows.

live module hub built dive ready soon specified

References

Sources

Related in this course