Hacker News new | past | comments | ask | show | jobs | submit login
Reader Conditionals in Clojure (clojure.org)
91 points by jballanc on March 31, 2015 | hide | past | favorite | 10 comments



As someone who uses cljx and leaves "lein cljx auto" and "lein cljsbuild auto" running in the background, how is my workflow going to change when I switch to reader conditionals?


If I understand right, you'll be able to eliminate the "lein cljx auto" and instead the reader(s) will be responsible for using the correct platform-specific snippet.


Correct.


For someone who is very new to Clojure, but trying to learn, can someone please explain what this is?


There are (currently) three host implementations of clojure: Clojure(JVM), ClojureCLR(.NET CLR), and ClojureScript(JavaScript). Supporting this reader conditional can facilitate code reuse as much as possible.


To expand on this a bit more: while all three implementations are "Clojure", since Clojure is a hosted language (i.e. doesn't specify/implement its own VM) and embraces this fact, there are subtle differences between the various implementations. Some of these differences could be viewed as "bugs" and should eventually go away (e.g. cannot use macros at runtime in CLJS), but some are inherent to the platform (e.g. how do you convert a string to an integer?).

Originally, if your code was simple enough and didn't touch any of the parts that were different between the implementations, then you could simply run the same code under Clojure, ClojureScript, and ClojureCLR. If it was even mildly involved, though, you'd have to write separate implementations for each.

Then the CLJX project came along. It introduced the `#+clj` and `#+cljs` forms that could be used to specify code that should only run under Clojure or ClojureScript respectively. For Clojure v1.7.0, the goal is to implement something similar in the core language. At first the plan was to simply adopt the CLJX forms directly, but as of 1.7.0-alpha6 there is this new concept of a conditional reader.

Ultimately, as a beginning Clojurist the impact to you should be small. Eventually, if you are looking to write libraries to be used across both Clojure and ClojureScript, this will be useful to reduce the amount of duplication in your project. As you move towards the heights of advanced Clojure usage, though, this turns out to be a very powerful concept that could potentially be useful for a wide range of applications.


Great answer.


I'd definitely recommend reading up on the Clojure Reader: http://clojure.org/reader

There is a big difference between Clojure/Lisp and other languages like Python or PHP. With a traditional language, the code is transformed via a parser into a data structure (typically as an abstract syntax tree) whereas with Clojure the code you are looking at _is_ the data structure of the program. This is why you'll oftentimes hear about Lispy languages modifying themselves or writing other programs. This is why writing a DSL in Clojure or Lisp is so natural or straightforward.

If you can wrap your head around this distinct difference I think that it will really help with understanding this story as well as a lot of the more fascinating differences with a Lispy languages.

For example, the explanation of the "Unknown or Expired Link" HN "bug": https://news.ycombinator.com/item?id=17705


Did they change the syntax to full forms? Previously they looked like cljx prefixes I thought. Looks much better imo.


Yes, the conditional part is always an expression or a spliced list.




Consider applying for YC's Spring batch! Applications are open till Feb 11.

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

Search: