Skip to content

Instantly share code, notes, and snippets.

@erhangundogan
Created October 4, 2021 16:33
Show Gist options
  • Select an option

  • Save erhangundogan/b7f853f2e736032e2ba421473bdcce0d to your computer and use it in GitHub Desktop.

Select an option

Save erhangundogan/b7f853f2e736032e2ba421473bdcce0d to your computer and use it in GitHub Desktop.
Polymorphic sum
module P = struct
type t =
| Null
| Int of int
| Float of float
let sum expr1 expr2 : t =
match expr1, expr2 with
| Int a, Int b -> Int (a + b)
| Float a, Float b -> Float (a +. b)
| _ -> Null
end
(*
utop # P.sum (P.Int 5) (P.Int 3);;
- : P.t = P.Int 8
utop # P.sum (P.Float 3.5) (P.Float 2.5);;
- : P.t = P.Float 6.
*)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment