Clojure中的协议问题:“没有单一的方法:......”错误

时间:2011-12-17 04:00:09

标签: clojure protocols

  

可能重复:
  Do clojure protocols allow one to have a variadic method the way funcions do (with an ampersand)?

使用协议运行一些Clojure代码时出现此错误:

Exception in thread "main" java.lang.IllegalArgumentException: No single method: add_component of interface: questar.entity.Entity found for function: add-component of protocol: Entity, compiling:(questar/entity.clj:17)

以下是相关代码:

(defprotocol Entity
  (add-component [this cname & args])
  (update-component [this cname f & args])
  (remove-component [this cname])
  (get-component [this cname])
  (has-component? [this cname]))

(extend-protocol Entity
  clojure.lang.IPersistentMap
    (add-component [this cname & args]
      (assoc entity cname (apply new-component cname args)))
    (update-component [this cname f & args]
      (let [component (get-component this cname)]
        (add-component this cname (apply f component args))))
    (remove-component [this cname]
      (dissoc this cname))
    (get-component [this cname]
      (this cname))
    (has-component? [this cname]
      (contains? this cname))

  clojure.lang.IDeref
    (add-component [this cname & args]
      (alter this (apply add-component cname args)))
    (update-component [this cname f & args]
      (alter this (apply update-component cname f args)))
    (remove-component [this cname]
      (alter this remove-component cname))
    (get-component [this cname]
      (get-component this @cname))
    (has-component? [this cname]
      (has-component? this @cname)))

我知道这可能与arity有关,但一切看起来都对我不对。这是因为它无法确定要调用哪个版本的add-component?

谢谢!

0 个答案:

没有答案