简体   繁体   English

在 Clojure 中,如何在不使用特殊点符号的情况下调用对象上的方法?

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

In Clojure I always use the .在 Clojure 中,我总是使用 . 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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