The extensible reader

Showing 1-4 of 4 messages
The extensible reader Brent Millare 3/17/12 2:05 PM
So I just watched the Clojure conj keynote 2011 and I am particularly interested in the extensible reader component. Is there anything of this available for testing in master or 1.4.0-beta*? What's the best way to pass around java types in the reader like <java.io.File>s?
Re: The extensible reader Brent Millare 3/18/12 6:07 PM
So to answer my own question, this is in 1.4.0.

Here is an example:

(binding [*data-readers* {'user/f (fn [x] (java.io.File. (first x)))}] (read-string "#user/f [\"hello\"]")) 

returns 

#<File hello> 

On the best way to pass types around, though, I still think this is an open question.

The problem is that, currently, the default print behavior of java objects is an unreadable form like


I think there should be discussion about defaulting this to

#java.class.name [args*]

this form is still unreadable by default, but with tagged literals, allows users to define custom reader behavior per class.

Either that or is there a way to alter the printing behavior of java objects locally, instead of globally as when defining a print-method for that type?
Re: The extensible reader Stuart Sierra 3/19/12 2:50 PM
On Sunday, March 18, 2012 9:07:54 PM UTC-4, Brent Millare wrote:

this form is still unreadable by default, but with tagged literals, allows users to define custom reader behavior per class.
#java.class.name [args*]



That syntax is already used for defrecords.

Clojure 1.4.0-beta2
user=> (defrecord Foo [a b])
user.Foo
user=> (read-string "#user.Foo[1 2]")
#user.Foo{:a 1, :b 2}
user=>

 
Either that or is there a way to alter the printing behavior of java objects locally, instead of globally as when defining a print-method for that type?


Sure, just define your own print function that defaults to the standard Clojure print.

-S

Re: The extensible reader Brent Millare 3/19/12 3:55 PM
 
That syntax is already used for defrecords.

Clojure 1.4.0-beta2
user=> (defrecord Foo [a b])
user.Foo
user=> (read-string "#user.Foo[1 2]")
#user.Foo{:a 1, :b 2}
user=>


Technically, there is no space with that method. As it is currently implemented, tags are separated by a space between the clojure data it is tagging.
 
Sure, just define your own print function that defaults to the standard Clojure print.


 That works! Some how the obvious can be quite elusive at times.