Skip to content

Instantly share code, notes, and snippets.

@aitoroses
Created April 3, 2019 07:14
Show Gist options
  • Select an option

  • Save aitoroses/5feacb62cd56aae5a8516e62bfd1b752 to your computer and use it in GitHub Desktop.

Select an option

Save aitoroses/5feacb62cd56aae5a8516e62bfd1b752 to your computer and use it in GitHub Desktop.
ReasonML adhoc polymorphism
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