Protocol equivalent of Python's `repr`?

248 views
Skip to first unread message

Sam Raker

unread,
Jul 13, 2014, 1:51:40 PM7/13/14
to clo...@googlegroups.com
I'm working my way through an intro to Clojure book, and I just got to the section on protocols. I might be missing something/committing a category error, but I'm wondering if there's a way to change the way a protocol is represented in the repl, akin to redefining `__repr__`/`__str__` in Python. The example protocol in the book involves a matrix (i.e., a vector of vectors). I can do this

(extend-protocol Matrix
    clojure.lang.IPersistentVector
    ...
    (pprint [vov] (clojure.pprint/pprint vov)))


which works fine when I call (pprint m) on a Matrix m:

(pprint m)
; [[0 0 0]
   [0 0 0]
   [0 0 0]]

What I'd like is to just have the same behavior "calling" the variable from the repl, i.e.

m
; [[0 0 0]
   [0 0 0]
   [0 0 0]]


Can/should this be done? Is this a job for macros? Am I just being lazy/un-Clojuric and just get over it and use (pprint m)?

James Reeves

unread,
Jul 13, 2014, 2:00:25 PM7/13/14
to clo...@googlegroups.com
The multimethods print-method and print-dup handle this.

- James


--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jony Hudson

unread,
Jul 13, 2014, 2:29:34 PM7/13/14
to clo...@googlegroups.com
You might also enjoy looking at Gorilla REPL [1], which has an easily extensible renderer [2] for just this sort of thing. Disclosure: I'm one of its authors!


Jony

Sam Raker

unread,
Jul 13, 2014, 3:36:00 PM7/13/14
to clo...@googlegroups.com
James: I haven't gotten to multimethods yet--I'll definitely check that out!

Jony: I use Gorilla REPL for basically all my clojure stuff already! It's really great, thanks so much. I know you can add stuff to project.clj to pretty-print everything in the repl, I was just wondering about targeting specific protocols and types.



--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to a topic in the Google Groups "Clojure" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/clojure/ClQ4LwjyxNo/unsubscribe.
To unsubscribe from this group and all its topics, send an email to clojure+u...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages