OCaml Option get

后端 未结 3 1369
不知归路
不知归路 2021-02-14 07:06

I\'m new to OCaml, I\'m trying to understand how you\'re supposed to get the value from an \'a option. According to the doc at http://ocaml-lib.sourceforge.net/doc/Option.html,

3条回答
  •  梦如初夏
    2021-02-14 07:27

    You should listen to the above posters advice regarding type safety but also be aware that unsafe function such as Option.get (which is available in batteries btw) are usually suffixed with exn. If you're curious this is how Option.get or Option.get_exn could be implemented then:

    let get_exn = function
      | Some x -> x
      | None   -> raise (Invalid_argument "Option.get")
    

提交回复
热议问题