by rbehrends · 10y ago · 0 comments
Example:
let f x = 2 * x + 1
let s = Marshal.to_string f [ Marshal.Closures ]
let g: (int -> int) = Marshal.from_string s 0
let () = Printf.printf "%d\n" (g 2)
Note: this may not work in the REPL and you may have to put it in a file and compile it.Note also that serialization need not be polymorphic. See e.g. the s-expression mechanism [2], which relies on metaprogramming facilities.
[1] http://caml.inria.fr/pub/docs/manual-ocaml/libref/Marshal.ht...
[2] https://realworldocaml.org/v1/en/html/data-serialization-wit...
utop # #require "core";;
utop # #require "core.syntax";;
utop # open Core.Std;;
utop # type foo = int * string with sexp;;
type foo = int * string
val foo_of_sexp : Sexp.t -> foo = <fun>
val sexp_of_foo : foo -> Sexp.t = <fun>
utop # sexp_of_foo (1, "foo");;
- : Sexp.t = (1 foo)