> > It turns out there are some subtle issues which can cause incorrect >> behavior were clojure.core/read to blindly wrap a PushbackReader around its >> argument: >> https://groups.google.com/d/msg/clojure/_tuypjr2M_A/W1EcEbMUg_cJ >> > > That sounds like magic. The user wrapping a PushbackReader around a > BufferedReader doesn't cause problems, but the library function doing so > does? Why would where the wrapping takes place make a difference? Is the > *only* problem the rare case of reading more than one object from the same > stream? A docstring warning to wrap manually *in those cases* would > suffice, then, no? >
It's not a question of *where* the wrapping takes place, it's a question of who holds a reference to the wrapped Reader and what their expectations are. The reason that clojure.core/read cannot wrap its argument in a PushbackReader is that it doesn't know who else holds a reference to that argument. Let's say I have a BufferedReader that initially contains the following two Edn elements: "string1""string2" Let's also say that I have a version of clojure.edn/read that accepts a BufferedReader and (unsoundly) wraps it in a PushbackReader. After the first read call, the PushbackReader clojure.core/read created will contain the following: "string2" But the BufferedReader might only contain the following: string2" Since the PushbackReader created by clojure.core/read will be garbage collected after the function returns, it will be impossible to parse any more data correctly out of this reader. And this won't even break all of the time--it will only *occasionally* break, in the cases where a non-whitespace element gets pushed back into the PushbackReader and then garbage collected. As for the motivation behind using a PushbackReader at all: http://en.wikipedia.org/wiki/Parsing#Lookahead -- 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 --- 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+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.