You often have to do bit of manual coercion to make things work nicely with 
the whole set of possible Clojure numerical types. Fortunately there are 
plenty of built-in functions in clojure.core to help you do this.

In your specific case I would do:

(.add (Complex. 1.0 2.0) (double 2)) 

This performs the coercion to the double type that you need for the Java 
interop. But the nice thing about the "(double ...)" is that it will handle 
not just longs but all other Clojure numerical types (ratios, bigints etc.)


On Saturday, 2 May 2015 19:53:12 UTC+8, Alan Forrester wrote:
>
> Hello 
>
> I'm currently trying to wrap org.apache.commons.math3.complex 
>
>
> http://commons.apache.org/proper/commons-math/apidocs/org/apache/commons/math3/complex/Complex.html
>  
>
> to make a complex number library and I have a problem. Many of the 
> methods won't work with all Clojure number types. For example, 
>
> (.add (Complex. 1.0 2.0) 2) 
>
> produces an IllegalArgumentException. The method only works if at 
> least one of the arguments is a complex number although the other can 
> be a double. 
>
> What would be the best way of handling this? Should I just wrap add 
> the way it is and explain what types are expected in the 
> documentation, or is there a better way of dealing with this issue 
> that would allow Clojure number types to be used seamlessly? 
>
> Alan 
>

-- 
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