tag of a variant and record type difference

11 views
Skip to first unread message

Kuba Roth

unread,
Oct 21, 2016, 12:32:12 AM10/21/16
to ocaml-core
Hello,
I don't quite understand the following error and was wondering what is the correct approach for pattern matching. I'd like to be able to to match a type and have a custom printer.
As far as i understand the T1 {x;} tag of a variant t2 is of the same t1 type. Why (in pattern matching) once "m" is matched with T1 I'm still not able to pass it to pprint function?
Thank you.

Error: This expression has type t2 but an expression was expected of type t1

type t1 = {
    x : int;
};;

type t2 =
    | T1 of t1
    | B of int
    | C of float;;
    
    
let a = {x=4};;
let b = T1 {x=4};;

let pprint m =
   print_int m.x;;


let pprintB m =
    match m with
    | T1 {x;} -> pprint m            (*  errors on m *)
    | _ -> print_string " exit "

Kuba Roth

unread,
Oct 21, 2016, 1:22:03 AM10/21/16
to ocaml-core
ahhh.... found the issue:
let pprintB m =
    match m with
    | T1 m -> pprint m
    | _ -> print_string " exit "

Malcolm Matalka

unread,
Oct 21, 2016, 1:34:43 AM10/21/16
to ocaml...@googlegroups.com

T1 r is not the same type, it wraps a value of t1.  So:

T1 t1 -> pprint t1


--
You received this message because you are subscribed to the Google Groups "ocaml-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ocaml-core+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages