I looked into this yesterday, and it seems to me that definline has
been broken since SVN rev 1065. I don't know why the usages of it in
core.clj don't cause errors at compile time, but they don't seem to
work quite right.
When used as a function, they work fine:
user=> (apply ints [(into-array Integer/TYPE [1 2 3])])
#<int[] [I@2f8b5a>
But when used as a macro, they return nil:
user=> (ints (into-array Integer/TYPE [1 2 3]))
nil
--Chouser
I wrote a new definline that seems to work correctly:
(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."
[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 ~args ~expr))
(var ~name))))
Using definline work, and return the Var, just like defn does:
user=> (definline inc2 "returns x plus 2" [x] `(+ 2 ~x))
#'user/inc2
The defined function works as a function:
user=> (map inc2 [1 2 3])
(3 4 5)
It also works inlined:
user=> (inc2 5)
7
And even the docstring works, which I think it didn't before:
user=> (doc inc2)
-------------------------
user/inc2
([x])
returns x plus 2
nil
This means that 'ints', 'doubles', and friends now work too:
user=> (doubles (into-array Double/TYPE [1 2 3]))
#<double[] [D@12aea3e>
user=> (doc doubles)
-------------------------
clojure.core/doubles
([xs])
Casts to double[]
nil
Patch attached.
--Chouser