LoginSignup
3
2

More than 5 years have passed since last update.

OCamlのPPX_Deriving

Last updated at Posted at 2014-12-23

この記事はAdvent Calender 2014 MLの19日目の記事です。

今日はPPX_DerivingっていうPPXを使った機能を使うと便利ですという話です。

URLはこちら: https://github.com/whitequark/ppx_deriving

opamでインストールできて

opam install ppx_deriving

For example, if you are using ocamlbuild, add the following to _tags to use the default deriving plugins:

<src/*>: package(ppx_deriving.std)

If you are using another buildsystem, just make sure it passes -package ppx_deriving_whatever to ocamlfind.

だそうです。試してないので試してみてください。<なんという手抜き記事

たとえば、以下のように書くと、HaskellやRustでおなじみのderiving showが出来ます!

type point2d = float * float
[@@deriving show]

素晴らしいppx!

追記:

test.ml
type point2d = float * float
[@@deriving show]

let _ =
    Printf.printf "%s\n" (show_point2d (1.1,2.2));

このようなファイルをコンパイルしたい場合

$ ocamlfind ocamlopt -package ppx_deriving.show test.ml -o test
$ ./test
(1.1, 2.2)

とします。ppx_deriving.showと.showを付けるのがポイントです。

OMakefileを使うなら

.PHONY: all install clean

USE_OCAMLFIND = true
OCAMLPACKS[] =
    ppx_deriving.show
FILES[] =
  test
PROGRAM = test
.DEFAULT: $(OCamlProgram $(PROGRAM), $(FILES))

とします。

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2