Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Option evaluation operator for F#
type OptionDefault<'a> =
| Default of 'a
| Lazy of (unit->'a)
| Expected of reason:string
let (|.) (opt:'a option) (def:OptionDefault<'a>) : 'a =
match opt with
| Some (v) -> v
| None ->
match def with
| Default(v) -> v
| Lazy(factory) -> factory ()
| Expected(reason) -> failwith reason
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment