-->

Why does Clojure lack user defined reader macros?

2019-05-16 16:36发布

问题:

As I understand it Clojure does not expose the reader macro table or allow user defined reader macros.

From http://clojure.org/reader:

The read table is currently not accessible to user programs.

I'm just wondering if there is a definitive or explicit statement (presumably from Rich Hickey) stating the rationale for leaving them out of Clojure.

Note I'm not asking if it is a good or bad thing that Clojure lacks user defined reader macros. Just wondering why.

回答1:

Speaking straight there are Tagged Literals that allow you to specify what to do with next form. For example, you can add

{to/u clojure.string/upper-case}

to data_readers.clj (see docs) and write something like this:

testapp.core> #to/u "asd"
"ASD"

but it's not so powerful as full support of reader macros, at least because of The data reader function is invoked on the form AFTER it has been read as a normal Clojure data structure by the reader.

I found this old log (don't ask me how) http://clojure-log.n01se.net/date/2008-11-06.html

where there is a discussion with Rich Hickey's thoughts about reader macros.