簡體   English   中英

在 Clojure 中,如何在不使用特殊點符號的情況下調用對象上的方法?

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

在 Clojure 中,我總是使用 . 符號即。

(.methCall obj args)

但是有沒有辦法在沒有點符號的情況下調用該方法? 例如。 當我轉身時,相當於“應用”之類的東西

(f x y) 

進入

(apply f x y)

?

我想這樣做是為了編寫一個宏,該宏可以將方法名稱作為參數之一並在對象上調用它。

有點特殊形式

例如

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

您正在使用的表單處於成員訪問權限下,並且是一個快捷方式:

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

以上文檔說明了以下擴展:

 (.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