Custom Type Polymorphic Map

type _ example_type =
  | IntList : int list -> int example_type
  | StringList : string list -> string example_type

let intlist = IntList [ 1; 2; 3 ]
let stringlist = StringList [ "a"; "b"; "C" ]

let map_test : type a. a example_type -> (a -> 'b) -> 'b list =
 fun x f ->
  match x with
  | IntList c -> List.map f c
  | StringList c -> List.map f c
;;

let _ = map_test intlist (fun x -> x + 1)
let _ = map_test stringlist (fun x -> String.capitalize_ascii x)

It is called GADT, more on Real World OCaml: GADTs - Real World OCaml

2 Likes