Good call on the auto-boxing.  I wasn't considering that before, but 
obviously it is important.

Nice insight into :inline.  I never really did understand the usefulness of 
it before.

On Tuesday, July 7, 2015 at 10:20:51 PM UTC-5, Herwig Hochleitner wrote:
>
> 2015-07-07 15:04 GMT+02:00 Jo Geraerts <j...@umask.net <javascript:>>:
>>
>> * 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
>  
> see also http://clojure.org/java_interop#Java 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
> Beware, that inline functions aren't public API and subject to change.
>
> kind regards
>

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to