On Wed, Jan 26, 2011 at 10:58 AM, Shantanu Kumar
<[email protected]> wrote:
> Hi,
>
> I have created some 'deftype' objects and 'extend'ed
> clojure.lang.ILookup to them to make them behave like maps. How can I
> get the clojure.pprint/pprint to pretty-print them like maps?
A deftype can override toString like this:
user=> (deftype Foo [n]
java.lang.Object
(toString [this] (str "Foo<" n ">")))
user.Foo
user=> (Foo. 3)
#<Foo Foo<3>>
user=>
It still gets printed the way generic objects normally do, though:
#<Classname resultOfCallingToString>.
Fortunately, pprint lets you override this behavior by adding methods
to a multimethod called *simple-dispatch*:
user=> (defmethod clojure.contrib.pprint/*simple-dispatch* Foo [x]
(print (.toString x)))
#<MultiFn clojure.lang.MultiFn@795f24>
user=> (Foo. 3)
Foo<3>
--
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