definline

clojure.core

  • (definline name & decl)
Experimental - like defmacro, except defines a named function whose
body is the expansion, calls to which may be expanded inline as if
it were a macro. Cannot be used with variadic (&) args.

0 Examples top

Log in to add / edit an example.

See Also top

Log in to add a see also.

Plus_12x12 Minus_12x12 Source clojure/core.clj:4546 top

(defmacro definline
  "Experimental - like defmacro, except defines a named function whose
  body is the expansion, calls to which may be expanded inline as if
  it were a macro. Cannot be used with variadic (&) args."
  {:added "1.0"}
  [name & decl]
  (let [[pre-args [args expr]] (split-with (comp not vector?) decl)]
    `(do
       (defn ~name ~@pre-args ~args ~(apply (eval (list `fn args expr)) args))
       (alter-meta! (var ~name) assoc :inline (fn ~name ~args ~expr))
       (var ~name))))
Vars in clojure.core/definline:
Used in 0 other vars

Comments top

2 comment(s) for definline.

Note that, as for macros, the arguments to definline are potentially subject to double evaluation if they are used more than once in the body. For example:

user=> (definline bad-sqr [x] `(* ~x ~x))
#'user/bad-sqr
user=> (bad-sqr (do (println "x") 5))
x
x
25

Any non-like-a-function behaviour should be avoided, because otherwise function will behave differently depending on whether it's inlined or not:

user=> (definline bad-if [cond then] `(if ~cond ~then))
#'user/bad-if
user=> (bad-if nil (do (prn :side-effect) :not-returned))
nil
user=> (let [bad-if bad-if] (bad-if nil (do (prn :side-effect)     :not-returned)))
:side-effect
nil
Log in to add a comment.