On Tue, Sep 22, 2009 at 1:31 AM, Jarkko Oranen <chous...@gmail.com> wrote:
> As Mike said, it's done by adding a method to print-method, but you > will need metadata... "structs" in clojure are nothing but an > optimisation of maps... That is, in all situations structmaps are (at > least, should be) interchangeable with a regular persistent map. They > do not create their own type or anything, so you can't override their > toString method directly. > > However, since print-method uses :type metadata, you can do the > following: > > ;; using ::foo to get namespace-qualified keywords is good for type > tags, because it avoids collisions > ;; typed-book could be a struct-map, or in fact any implementation of > Map that also supports metadata > (def typed-book (with-meta {:book "somebook"} {:type ::my-book})) ; > the latter map is the metadata > (defmethod print-method ::my-book [thebook writer] > (print-method (:book thebook) writer)) ; falls back to the string > writer > > (print typed-book) ; prints 'somebook' Thank you Mike and Jarkko for the detailed answer. It works as you described. :) Sincerely, Jung Ko --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---