Search code examples
ocamldeclarationexplicit

Ocaml - how do I explicitly declare the list mutable type


I tried something like:

let lchars : char ref list = ref [];;

but don't work...


Solution

  • Parameterized OCaml types are specified in postfix order, so the type char ref list is first and foremost a list. In other words, it's a list of references to char:

    let (x: char ref list) = [ ref 'a'; ref 'b']
    

    Similarly, the type char list ref is first and foremost a reference. It's a reference to a list of chars:

    let (y: char list ref) = ref ['a'; 'b']
    

    You wanted the second of these types but your code specifies the first type.