I just finished my first macro:
(defmacro defrecord-withstr [recordname include columns recordargs]
(let [ vv (gensym)
stato
(let [cols
(if include
columns
(vec(seq(clojure.set/difference (set recordargs) (set
columns)))))
]
(vec(interleave
(vec(map #(str ":" %) cols))
(vec(map #(symbol(str "(str (:" % " " vv "))"))
cols
))
)))]
`(defrecord ~recordname [~@recordargs]
Object
(~(symbol "toString") [~vv]
(reduce
#(str %1 " " %2)
~stato
)
)))
)
It should override the toString for records so that it prints just the
chosen columns.
If i do the macroexpand:
user> (macroexpand-1 '(defrecord-withstr Test true [a b] [a b c d]))
(clojure.core/defrecord Test [a b c d] java.lang.Object (toString
[G__7830] (clojure.core/reduce (fn* [p1__7524__7526__auto__
p2__7525__7527__auto__] (clojure.core/str p1__7524__7526__auto__ " "
p2__7525__7527__auto__)) [":a" (str (:a G__7830)) ":b" (str (:b
G__7830))])))
When I try to execute that everything works OK:
user> (clojure.core/defrecord Test [a b c d] java.lang.Object
(toString [G__7830] (clojure.core/reduce (fn* [p1__7524__7526__auto__
p2__7525__7527__auto__] (clojure.core/str p1__7524__7526__auto__ " "
p2__7525__7527__auto__)) [":a" (str (:a G__7830)) ":b" (str (:b
G__7830))])))
user.Test
user> (def tt (Test. 1 2 3 4))
#'user/tt
user> tt
#:user.Test{:a 1, :b 2, :c 3, :d 4}
user> (str tt)
":a 1 :b 2"
But when I call this directly, it doesn't work:
user> (defrecord-withstr Test2 true [a b] [a b c d])
Unable to resolve symbol: (str (:a G__7900)) in this context
[Thrown class java.lang.Exception]
Restarts:
0: [QUIT] Quit to the SLIME top level
Backtrace:
0: clojure.lang.Compiler.resolveIn(Compiler.java:5677)
1: clojure.lang.Compiler.resolve(Compiler.java:5621)
2: clojure.lang.Compiler.analyzeSymbol(Compiler.java:5584)
3: clojure.lang.Compiler.analyze(Compiler.java:5172)
4: clojure.lang.Compiler.analyze(Compiler.java:5151)
5: clojure.lang.Compiler$VectorExpr.parse(Compiler.java:2591)
6: clojure.lang.Compiler.analyze(Compiler.java:5192)
7: clojure.lang.Compiler.analyze(Compiler.java:5151)
8: clojure.lang.Compiler$InvokeExpr.parse(Compiler.java:3057)
9: clojure.lang.Compiler.analyzeSeq(Compiler.java:5371)
10: clojure.lang.Compiler.analyze(Compiler.java:5190)
It must be some namespace issue, but I just don't understand what is
the problem...
Thx
Dusan
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en