We have a simple function definition:
(defn calculate [d x y]
((if (and (== d 1) (== x 1) (== y 0))
1
0)))
(println (calculate 1 1 0))
But that results in this error:
Exception in thread "main" java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:616)
at jline.ConsoleRunner.main(ConsoleRunner.java:69)
Caused by: java.lang.ClassCastException: java.lang.Long cannot be cast to clojure.lang.IFn
at user$calculate.invoke(main.clj:13)
at user$eval5.invoke(main.clj:17)
at clojure.lang.Compiler.eval(Compiler.java:6465)
at clojure.lang.Compiler.load(Compiler.java:6902)
at clojure.lang.Compiler.loadFile(Compiler.java:6863)
at clojure.main$load_script.invoke(main.clj:282)
at clojure.main$script_opt.invoke(main.clj:342)
at clojure.main$main.doInvoke(main.clj:426)
at clojure.lang.RestFn.invoke(RestFn.java:408)
at clojure.lang.Var.invoke(Var.java:401)
at clojure.lang.AFn.applyToHelper(AFn.java:161)
at clojure.lang.Var.applyTo(Var.java:518)
at clojure.main.main(main.java:37)
... 5 more
(BTW, line 13 is the line with if and and).
The same with vectors:
(defn calculate [vectorr]
((if (and (== (vectorr 0) 1) (== (vectorr 1) 1) (== (vectorr 2) 0))
1
0)))
(println (calculate [1 1 0]))
This results in:
Caused by: java.lang.ClassCastException: java.lang.Long cannot be cast to clojure.lang.IFn
… the same.
And with a map:
(defn calculate [mapp]
((if (and (== (mapp :d) 1) (== (mapp :x) 1) (== (mapp :y) 0))
1
0)))
(println (calculate {:d 1 :x 1 :y 0}))
Results in the same message:
Caused by: java.lang.ClassCastException: java.lang.Long cannot be cast to clojure.lang.IFn
We think is has something to do with if, or the comparisons. We have tried it with normal arguments, a vector as an argument and with a map as such and obviously, the error stays the same.
We’ve also tried many other variations without greater insights on the actual problem.
You have an extra pair of parentheses around your function body, which means you want to call the result of the
if(which is either 1 or 0 — i.e. a long).It should be: