-
-
Save erhangundogan/b7f853f2e736032e2ba421473bdcce0d to your computer and use it in GitHub Desktop.
Polymorphic sum
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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