Skip to content

Instantly share code, notes, and snippets.

@pasberth
Created March 29, 2014 12:14
Show Gist options
  • Save pasberth/9853427 to your computer and use it in GitHub Desktop.
Save pasberth/9853427 to your computer and use it in GitHub Desktop.
多相バリアントで Show 的な
type 'a show = [`Show of (string * 'a)]
type my_int = [`Show of (string * int)]
type my_float = [`Show of (string * float)]
type my_str = [`Show of (string * string)]
let show (`Show (pretty, x) : [< `Show of (string * 'a) ]) = pretty
let print x = print_endline (show x)
let mk_my_int x = `Show((string_of_int x), x)
let mk_my_float x = `Show((string_of_float x), x)
let mk_my_str x = `Show(("\""^x^"\""), x)
let () = begin
let x = mk_my_int 42 in
let y = mk_my_float 3.14 in
let z = mk_my_str "answer" in
print x;
print y;
print z;
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment