Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

TXR Lisp:

  1> (defstruct (word text) ()
       text
       (:method print (me stream pretty-p) (put-string `#<@(typeof me) @{me.text}>` stream))
       (:method equal (me) me.text))
  #<struct-type word>
  2> (new (word "abc"))
  #<word abc>
  3> (equal *2 "abc")
  t
You wouldn't define a print method for a struct like this, because it's counterproductive. You're throwing away print-read consistency in exchange for no benefit.

I usually define print methods for complex structures with many slots, that are not expected to be externalized. Particularly if they are linked in a graph structure, where (even if you have the circular printing turned on to handle the cycles) the prints get large. You know, you wanted to just print the banana, but it was pointing to a gorilla, and basically a dump of the whole jungle ensued.



You wouldn't define an `inspect` method for this simple example in Ruby either, because Ruby provides a default inspect that'd do just fine in this example. The exact same tradeoff for when to define inspect as for when you'd define print methods exist. In other words, that wasn't the point.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: