clojurejava-interopclojure-java-interop

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


Solution

  • 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))))