Herwig Hochleitner schreef op 2015-07-08 05:20:
> 2015-07-07 15:04 GMT+02:00 Jo Geraerts <
j...@umask.net [1]>:
>
>> * multiply(long x)
>> * multiply(double x)
>> * multiply(Number x)
>>
>> In clojure i want to do something like
>>
>> (defn multiply[^MonetaryAmount amount multiplicant]
>> (.multiply amount multiplicant))
> Function parameters in Clojure, are generally passed as a
> java.lang.Object, so numbers are boxed by default.
> Clojure does have infrastructure to pass primitive numbers, see
> invokePrim
>
> here:
https://github.com/clojure/clojure/blob/master/src/jvm/clojure/lang/IFn.java#L97
> [2] see also
http://clojure.org/java_interop#Java [3] Interop-Support
> for Java Primitives
> however, this requires a specific type hint on the multiply fn, so
> normally that means separate multiply-double and multiply-long fns.
> The way I would do it: Define multiply as a function calling
> (.multiply amount ^Number x), for higher order usage, and then add an
> :inline function to its metadata, which returns `(.multiply ~amount
> ~x).
> That acts as a compiler macro, which inlines the call to .multiply,
> that way, its parameter type can be assigned via local type
> inferrence
> (which clojure does).
>
> See
http://www.bytopia.org/2014/07/07/inline-functions-in-clojure/#sec-3
> [4]
> Beware, that inline functions aren't public API and subject to
> change.
This is enlightening. I found such constructs in clojure's core.clj
cause there similar things should happen for certain functions, but i
couldn't fully grasp what was going on. In core.clj also quite some
dispatching happens to clojure.lang.RT which i figured was part of the
:inline setup.
Thank you for the explanation.
Kr,
Jo