Search code examples
typesf#option-typemutable

How do I create an F# mutable option type?


I need to create a mutable option<T> type in F#. I've tried writing

let x = ref None

and subsequently writing

x := Some(z)

but it doesn't work. Help!


Solution

  • You need to state the type explicitly to avoid "the Value Restriction" (or see "Automatic Generalization" on msdn):

    let x : Ref<int option> = ref None
    
    x := Some 4