Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to pass java member function as a var

Tags:

clojure

If I run the following code in the REPL

(let [f '.startsWith] (f "abab" "a"))

it is evaluated to "a" instead of 'true'. Could someone please explain me this surprising result?

Actually, the real code, I want to make work is the following.

(defn set-up-bean! [bean functions-and-parameters]
  (doseq [[f p] functions-and-parameters]
    (f bean p))
  (.init bean))

What I want to achieve is, to make the following two function calls do the same thing.

(set-up-bean! bean [['.setMember "a"]])

and

(do
  (.setMember bean "a")
  (.init bean))
like image 305
korommatyi Avatar asked Jul 28 '26 17:07

korommatyi


1 Answers

One conventional approach is to use an anonymous function

(let [f (fn [a b] (.startsWith ^String a ^String b))] (f "abab" "a"))

...as this lets you type-hint parameters as-needed. You might also consider memfn:

(let [f (memfn startsWith String)] (f "abab" "a"))

In any event -- dot notation is syntactical sugar for interop, rather than providing real callable functions.

like image 163
Charles Duffy Avatar answered Jul 30 '26 09:07

Charles Duffy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!