Tries to pass java member function as var

If I run the following code in the REPL

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

      

evaluates to "a" instead of "true". Can anyone explain this unexpected result to me?

Actually, the real code I want to do the job 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. Same.

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

      

and

(do
  (.setMember bean "a")
  (.init bean))

      

+3


source to share


1 answer


One common approach is to use an anonymous function

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

      

... as it allows you to enter prompt parameters as needed. You can also consider memfn :



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

      

Anyway - dot notation is syntactic sugar for interaction, not providing real callable functions.

+6


source







All Articles