2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Clojureでproxyオブジェクトにメタ情報を付与する

Posted at

reifyで作成したオブジェクトはclojure.lang.IObjを実装しているのでwith-metaでメタ情報を付与できますが、proxyで作成したオブジェクトはそうはいきません。

(def v (proxy [Object] []
              (toString [] (str :abc))))
(with-meta v {:A 111})
java.lang.ClassCastException: user.proxy$java.lang.Object$ff19274a cannot be cast to clojure.lang.IObj

以下のようにclojure.lang.IObjのwithMetaとmetaを実装すればwith-metaでproxyオブジェクトにもメタ情報を付与できます。

(def v (proxy [Object clojure.lang.IObj] []
              (toString [] (str :abc))
              (withMeta [m] (update-proxy this {"meta" (fn [this] m)}))
              (meta [] nil)))
(with-meta v {:A 111})
(meta v)

ただし、通常のwith-metaとは違ってメタ情報はmutableに変更されます。

2
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
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?