In Clojure, how can I call a method on an object without using special dot notations?

Viewed 90

In Clojure I always use the . notation ie.

(.methCall obj args)

But is there a way to call the method without having that dot notation? Eg. the equivalent of something like "apply" when I turn

(f x y) 

into

(apply f x y)

?

I wanna do this in order to write a macro that can take a method name as one of the arguments and call it on an object.

1 Answers

There is the dot special form

E.g.

user=> (. (bigint 42) toString)
"42"

The form you are using is under member access and is a shortcut:

user=> (macroexpand '(.toString (bigint 42)))
(. (bigint 42) toString)

Above docs state the following expansions:

(.instanceMember instance args*) ==> (. instance instanceMember args*)
(.instanceMember Classname args*) ==> (. (identity Classname) instanceMember args*)
(.-instanceField instance) ==> (. instance -instanceField)
(Classname/staticMethod args*) ==> (. Classname staticMethod args*)
Classname/staticField ==> (. Classname staticField)
Related