简体   繁体   中英

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

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.

There is the dot special form

Eg

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM