Fsharp: None, Some

By Xah Lee. Date: .
work in progress

What's none and some

in Fsharp, what's None and Some

(ai answer)

In F#, None and Some are the two cases of the built-in Option<'T> type (a discriminated union).

type Option<'T> =
    | None
    | Some of 'T

Typical use:

let maybeNumber: int option = Some 42
let nothing: int option = None

match maybeNumber with
| Some n -> printfn "Got %d" n
| None   -> printfn "Nothing"

It’s F#’s way of representing optional values (similar to Maybe in Haskell or Optional in other languages), avoiding nulls.

work in progress

is Option<'T> a type sig, what is it

(ai answer)

Yes — Option<'T> (or option<'T>) is a type.

It is a generic discriminated union defined roughly as:

type Option<'T> =
    | None
    | Some of 'T

Examples:

let a: int option = Some 42
let b: string option = None
let c: option<float> = Some 3.14   // same thing

So Option<'T> is the type name; None and Some are its two cases.