算術式を表すインターフェースを実装しようとしています。インターフェイスは Java 側で使用されますが、ロジック全体は clojure にあります。
持つ:
(defprotocol ExtendsExpression
(toTree [this]))
(extend-type String
ExtendsExpression
(toTree [this] (symbol this)))
(extend-type Number
ExtendsExpression
(toTree [this] this))
(definterface Expression
(toTree []))
(defrecord Expression1 [^String oper arg]
Expression
(toTree [this]
(list (symbol oper) (toTree arg))))
(defrecord Expression2 [^String oper arg1 arg2]
Expression
(toTree [this]
(list (symbol oper) (toTree arg1) (toTree arg2))))
(defrecord Expression3 [^String oper arg1 arg2 arg3]
Expression
(toTree [this]
(list (symbol oper) (toTree arg1) (toTree arg2) (toTree arg3))))
私はそれを次のように使用しようとします:
(toTree (Expression3. "+" "a" "b" (Expression2. "*" "c" "d")))
しかし、私は得ています:
IllegalArgumentException No implementation of method: :toTree of protocol: #'user/ExtendsExpression found for class: user.Expression3 clojure.core/-cache-protocol-fn (core_deftype.clj:541)
clojure が Expression3 の ExtendsExpression の toTree を呼び出そうとするのはなぜですか? Expression3 では、Expression インターフェイスの toTree メソッドが呼び出されることを期待しています。