Clojure, unquote slicing outside of syntax quote

Viewed 733

In clojure, we can use unquote slicing ~@ to spread the list. For example

(macroexpand `(+ ~@'(1 2 3))) 

expands to

(clojure.core/+ 1 2 3)

This is useful feature in macros when rearranging the syntax. But is it possible to use unquote slicing or familiar technique outside of macro and without eval?

Here is the solution with eval

(eval `(+ ~@'(1 2 3))) ;-> 6

But I would rather do

(+ ~@'(1 2 3))

Which unfortunately throws an error

IllegalStateException Attempting to call unbound fn: #'clojure.core/unquote-splicing  clojure.lang.Var$Unbound.throwArity (Var.java:43)

At first I thought apply would do it, and it is indeed the case with functions

(apply + '(1 2 3)) ; -> 6

However, this is not the case with macros or special forms. It's obvious with macros, as it's expanded before apply and must be first element in the form anyway. With special forms it's not so obvious though, but still makes sense as they aren't first class citizens as functions are. For example the following throws an error

(apply do ['(println "hello") '(println "world")]) ;-> error

Is the only way to "apply" list to special form at runtime to use unquote slicing and eval?

1 Answers
Related