coreのsexplibを使ってみる

ppx_derivingにこだわっていたけれど。coreを使っているならsexplibでserializeしても良いような気がした。

詳しくはreal world ocamlに書いてあった記憶。

opam install core sexplib

field.ml

open Core_kernel.Std
module S = Sexp

type point2d = P of int * int with sexp
type 'a _list = Nil | Cons of 'a * 'a _list with sexp

let () =
  let p = P (10, 20)
  in
  p |> sexp_of_point2d |> S.to_string |> print_string;
  print_newline ();

  let tri = Cons (1, Cons (2, Cons (3, Nil)))
  in
  tri |> <:sexp_of<int _list>> |> S.to_string |> print_string;
  print_newline ();

Makefile

field.native:
    @corebuild field.native

clean:
    rm -r *.native _build

.PHONY: clean
$ make
$ ./field.native
(P 10 20)
(Cons 1(Cons 2(Cons 3 Nil)))