Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are Clojure macros always leaky?

Tags:

macros

clojure

If you read the question macro -> with anonymous functions you'll see that the -> macro doesn't play well with anonymous functions. To use the macro correctly you need to understand the implementation. In that sense, the macro is "leaky" - the implementation is not completely hidden by the API.

Is it always that case that a (sufficiently complex) Clojure macro is leaky?

[For comparison: A similar issue occurs with the C pre-processor, where strange side effects can be seen when macro arguments are handled carelessly. In that case, issues can be solved by enclosing the macro arguments in parentheses (inside the macro). That does not solve the issue of using C macros with mutable state (ie where each use of the argument mutates state), but perhaps we can either ignore that issue for functional languages, or use let to avoid multiple evaluations.]

like image 462
andrew cooke Avatar asked Dec 15 '22 23:12

andrew cooke


2 Answers

You don't need to understand the implementation - the docstring is quite clear as to how it works. Reader macros are also well documented - #(...) will expand to (fn [..] ...). Given this knowledge and the information provided docstring it's clear that threading anonymous functions won't work. No need to understand the implementation at all.

like image 94
dnolen Avatar answered Dec 20 '22 12:12

dnolen


Clojure macros aren't leaky in that sense. The reason -> works sort of unexpectedly with #() functions is that #() is a reader macro and reader macros are expanded before "regular" macros. So you need to know:

  1. What the macro is supposed to do. -> is actually a very basic macro in that the documentation pretty much explains exactly how it expands.
  2. What a reader macro expands into if you want to pass it to a "normal" macro.
like image 26
Joost Diepenmaat Avatar answered Dec 20 '22 11:12

Joost Diepenmaat