lllllllllllll
lllllllllllll

Reputation: 9110

Pattern match on records with option type entries in OCaml

basically I have defined a record type like this:

and exp = Bil_t.exp = {
  var: var option;
  binop: binop option;
  load: load option;
  store: store option;
  cast: cast option;
  inte: inte option;
  let_exp: let_exp option
}

And I am thinking to use a pattern match to process it, something like this:

match rexp with
    | {None;binop;None;None;None;None;None} -> trans_binop @@ strip binop
    | {var;None;None;None;None;None;None} -> BU.inte_to_string @@ strip @@ mark inte
    | _ -> failwith "undefined"

Sorry for the messy code above. So basically I compile the above code, and I get the error:

Error: Syntax error

Could anyone give me some help on this.. I just don't know what is wrong here...

Upvotes: 1

Views: 1003

Answers (1)

Jeffrey Scofield
Jeffrey Scofield

Reputation: 66818

Record patterns need to include the field names.

type exp = {
  var: int option;
  binop: int option;
  load: int option;
  store: int option;
  cast: int option;
  inte: int option;
  let_exp: int option
}

let f rexp =
    match rexp with
    | { var = None; binop = Some b; load = None; store = None;
        cast = None; inte = None; let_exp = None
      } -> b
    | _ -> failwith "undefined"

Example:

# let r = { var = None; binop = Some 14; load = None; store = None; 
            cast = None; inte = None; let_exp = None };;
val r : exp =
  {var = None; binop = Some 14; load = None; store = None; cast = None;
   inte = None; let_exp = None}
# f r;;
- : int = 14

Upvotes: 3

Related Questions