The extensible reader

285 views
Skip to first unread message

Brent Millare

unread,
Mar 17, 2012, 5:05:40 PM3/17/12
to clo...@googlegroups.com
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?

Brent Millare

unread,
Mar 18, 2012, 9:07:54 PM3/18/12
to clo...@googlegroups.com
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?

Stuart Sierra

unread,
Mar 19, 2012, 5:50:19 PM3/19/12
to clo...@googlegroups.com
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

Brent Millare

unread,
Mar 19, 2012, 6:55:31 PM3/19/12
to clo...@googlegroups.com
 
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.
Reply all
Reply to author
Forward
0 new messages