Record pattern matching

f#, functional-programming, ocaml, pattern-matching, record

Solution

It's probably not helpful to treat OCaml and F# as the same language. Your code is invalid OCaml for several reasons.

But you're right, the `_` is not necessary in OCaml. It is useful if you want to get a warning for incomplete record patterns. If you mark intentionally incomplete record patterns with `_` and turn on warning 9, then record patterns without `_` will be flagged if they don't specify all the fields of the record.

$ rlwrap ocaml -w +9
        OCaml version 4.03.0

# type t = { a: int; b: string};;
type t = { a : int; b : string; }
# let f {a = n} = n;;
Warning 9: the following labels are not bound in this record pattern:
b
Either bind these labels explicitly or add '; _' to the pattern.
val f : t -> int = <fun>

It was fairly hard to find the documentation for this. You can find it in Section 7.7 of the OCaml manual. It's listed specifically as a language extension.

Problem

According to this accepted answer, in F# and OCaml I need to use underscore for discarding the rest of the record. However, why does the `handle'` function work but `handle` function doesn't? ``` type Type = Type of int type Entity = { type' : Type foo : string } let handle entities = match entities with | {type' = Type i; _ }::entites -> () | [] -> () let handle' entities = match entities with | {type' = Type i }::entites -> () | [] -> () ```

Original source

Related problems