• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Clojure 1.4 Extensible Reader

 
clojure forum advocate
Posts: 3479
Mac Objective C Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Chas,
Clojure 1.4 introduces "Extensible Reader". What is it and what it does bring to the table?
Thank you.
 
author
Posts: 27
Clojure
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Prior to Clojure 1.4.0, the reader only ever emitted a fixed set values, including scalars, vectors, maps, and so on. (There are some ill-advised ways around this, but they are largely made irrelevant by 1.4's extensible reader.)

With Clojure 1.4.0, you can control how the reader produces values for literals. This doesn't allow you to change syntax arbitrarily (i.e. all forms still need to be s-expressions consisting of scalars, lists, sets, and so on), but it does allow you to specify any function you like that will, at read-time, be able to control what value comes out of the other end of the reader.

For example, Clojure does not currently define built-in syntax for URIs or URLs. This could make things more cumbersome in your application if you work with URIs a lot, as you'd always have to manually use java.net.URI to parse strings containing URIs, and then producing strings again if you needed to send them to storage or across the wire to a client. With the extensible reader, you could specify URIs using syntax like:



…where the #net/uri tag would correspond with a function that the reader would invoke with the value immediately after the tag in order to determine what the reader will return for that value. The function corresponding with #net/uri might be something as simple as returning a URI instance:



…and so, with this in place, the reader would return a java.net.URI value anywhere the #net/uri tag is used, without any additional work on your part.

To learn more, read the 1.4.0 changelog sections related to the extensible reader, which talk about how to get your reader functions mapped to tags, and various other details.

--
(coauthor of Clojure Programming from O'Reilly; creator of Clojure Atlas)
 
"I know this defies the law of gravity... but I never studied law." -B. Bunny Defiant tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic