开发者

clojure -- name conflicts in a defined record?

I have a misbehaving piece of code; when I name a record MethodInfo, it no longer overrides the .toString method correctly.

(defrecord MethodInfo [^clojure.lang.ISeq x ^clojure.lang.ISeq y]
  java.lang.Object
    (toString [x]
      (str (:x x))))

Running a simple test shows how this fails,

=> (.toString (new MethodInfo [1 2] [3]))
"sketch.compiler.main.sklojure1.MethodInfo@10e0d118"

whereas renaming the record to A shows the code behaving correctly,

开发者_运维知识库=> (.toString (new A [1 2] [3]))
"[1 2]"

what am I doing wrong??


Your record works fine for me. I would recommend restarting the REPL as there might be some old code hanging around. Note also that you have direct access to fields in the record, so you can write

(defrecord MethodInfo [x y]
  Object
  (toString [_] (str x)))

instead of

(defrecord MethodInfo [x y]
  Object
  (toString [this] (str (:x this))))
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜