On Tue, Oct 25, 2011 at 14:45, Sean Devlin <francoisdev...@gmail.com> wrote:
> I was wondering if there was a common protocol to get a string
> representation of an object yet.  Also, are there common protocols for
> ints, doubles, chars, etc?  Having just spent a lot of time writing
> Python, having an int function that worked on both Strings and Ints
> was great.  I'd love to be able to use a protocol for this.


What do you need the string for? Just for presentation (like __str__),
or are you hoping to be able to parse it back in again (like
__repr__)?

(pr-str x) will produce a string representation of x which you can
convert back to Clojure data using (read-string s) provided x was
composed only of things that are representable as Clojure literals
(lists, vectors, maps, sets, keywords, strings, symbols, numbers,
chars).

The behavior of pr and friends is not defined in a protocol, as such,
but rather by the multimethods print-method and print-dup. I suppose
one could add behavior for ones own types, but this would be of
limited utility since read and friends won't know how to read these
representations back in.

(str x) produces a string for presentation. This is not necessarily
something that can be read back in.

This is not defined by a protocol, as such, but rather by overriding
the toString(), which all Java classes provide.

// Ben

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@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+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to