Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationEventAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
2
Help us understand the problem. What are the problem?

More than 5 years have passed since last update.

posted at

updated at

OCamlのPPX_Deriving

この記事は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))

とします。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
2
Help us understand the problem. What are the problem?