Fsharp: None, Some
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).
None— no value is present.Some value— a value of type'Tis present.
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.
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
'Tis a type parameter (any type:int,string, your own type, etc.).- A value of type
Option<'T>is eitherNoneorSomecontaining a value of type'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.