OCaml Option get

后端 未结 3 1364
不知归路
不知归路 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:32

    The usual way to do this is with pattern matching.

    # let x = Some 4;;
    val x : int option = Some 4
    
    # match x with
      | None -> Printf.printf "saw nothing at all\n"
      | Some v -> Printf.printf "saw %d\n" v;;
    saw 4
    - : unit = ()
    

    You can write your own get function (though you have to decide what you want to do when the value is None).

提交回复
热议问题