Created
April 3, 2019 07:14
-
-
Save aitoroses/5feacb62cd56aae5a8516e62bfd1b752 to your computer and use it in GitHub Desktop.
ReasonML adhoc polymorphism
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 type SHOW = { | |
| type t | |
| let show: t => string | |
| }; | |
| type show_impl('a) = (module SHOW with type t = 'a) | |
| module Show_int: SHOW with type t = int = { | |
| type t = int; | |
| let show = string_of_int | |
| }; | |
| module Show_bool: SHOW with type t = bool = { | |
| type t = bool; | |
| let show = string_of_bool | |
| }; | |
| let show_int: show_impl(int) = (module Show_int: SHOW with type t = int) | |
| let show_bool: show_impl(bool) = (module Show_bool: SHOW with type t = bool) | |
| let print = (type a, show, x) => { | |
| let module Show = (val (show: (module SHOW with type t = a))); | |
| print_endline @@ Show.show(x); | |
| }; | |
| true |> print(show_bool); | |
| false |> print(show_bool); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment