なんとな~くしあわせ?の日記

「そしてそれゆえ、知識そのものが力である」 (Nam et ipsa scientia potestas est.) 〜 フランシス・ベーコン

ClojureでJavaの無名クラス実装

nantonaku-shiawase.hatenablog.com

以前JavaApache POIでCellWalkContextの実装を作ったのだが、これをClojureでやる場合はproxyを使うのがよい。

Java実装

		CellWalk cw = new CellWalk(sheet, range);
		cw.traverse(new CellHandler() {
			@Override
			public void onCell(Cell c, CellWalkContext ctx) {
				c.setCellType(CellType.BLANK);
			}
		});

Clojure実装

    (.traverse cw (proxy [CellHandler] []
                    (onCell [^Cell c ^CellWalkContext ctx]
                      (.setCellType c CellType/BLANK)
                      )))

    ))

Clojureにはproxyとreifyというものがあるが、Javaのメソッドに実装を入れたオブジェクトを突っ込む場合はproxyで動いた。

What is the difference between proxy and reify?