Created
February 8, 2012 22:16
-
-
Save einblicker/1774481 to your computer and use it in GitHub Desktop.
OCamlでも型安全なprintf
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
let dummyVal () = failwith "dummy";; | |
class virtual ['a] dir (dummy : (unit -> 'a)) = object | |
method virtual format_aux : string -> 'a | |
end;; | |
class d_end = object | |
inherit [string] dir dummyVal | |
method format_aux = | |
fun out -> out | |
end;; | |
class ['a] d_lit (s : string) (d : 'a dir) = object | |
inherit ['a] dir dummyVal | |
method format_aux = | |
fun out -> d#format_aux (out ^ s) | |
end;; | |
class ['a] d_int (d : 'a dir) = object | |
inherit [int -> 'a] dir dummyVal | |
method format_aux = | |
fun out i -> d#format_aux (out ^ Printf.sprintf "%d" i) | |
end;; | |
class ['a] d_str (d : 'a dir) = object | |
inherit [string -> 'a] dir dummyVal | |
method format_aux = | |
fun out s -> d#format_aux (out ^ s) | |
end;; | |
let l s d = new d_lit s d;; | |
let s d = new d_str d;; | |
let i d = new d_int d;; | |
let e () = new d_end;; | |
let format d = d#format_aux "";; | |
format (l "hello, " (s (l "!" (e ())))) "world";; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment