无法理解点(`.`)特殊形式的参数
在测试 java 互操作性时,我执行了 - (macroexpand-1 '(.toUpperCase "deepak"))
,输出为 (."deepak" toUpperCase)
现在,我知道 .
执行某种操作。
当我使用 (clojure.repl/doc .)
打印 .
的 doc
时,我得到了输出,因为
-------------------------
.
(.instanceMember instance args*)
(.instanceMember Classname args*)
(Classname/staticMethod args*)
Classname/staticField
Special Form
The instance member form works for both fields and methods.
They all expand into calls to the dot operator at macroexpansion time.
我无法理解.
期望的参数。例如 -
当我们执行 (. "deepak “ toUpperCase)
?
表达式 - (. "deepak" toUpperCase)
的实例名称、类名称和实例是什么?
While testing the java interoperability, I executed - (macroexpand-1 '(.toUpperCase "deepak"))
and the output was (. "deepak" toUpperCase)
Now, I came to know that .
performs some sort of operation.
When I printed the doc
for .
using (clojure.repl/doc .)
, I got the output as
-------------------------
.
(.instanceMember instance args*)
(.instanceMember Classname args*)
(Classname/staticMethod args*)
Classname/staticField
Special Form
The instance member form works for both fields and methods.
They all expand into calls to the dot operator at macroexpansion time.
I could not understand the pattern of parameters the .
expects. For example - .instanceMember
How does the parameters "deepak"
and toUpperCase
map to the above pattern when we execute (. "deepak" toUpperCase)
?
What is instance name, class name, and instance for the expression - (. "deepak" toUpperCase)
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您将在这里深入研究 Clojure 的内部细节。这通常不是必需的(并且通常没有用)。
像
(.toUpperCase "deepak")
这样的表达式调用 java.lang.String 对象"deepak"
上的方法 "toUpperCase",例如编译器在内部将上面的内容翻译成
这当然有相同的结果。
您可以在 The Dot Special Form 的文档中找到更多信息
这些文档有时很难阅读,但是你可以通过一些模式匹配来破译它。在上面的示例中,
.toUpperCase
是“.instanceMember”,“deepak”
是java.lang.String
类型的“instance” >。对于
(.getName String)
,符号String
(java.lang.String
的简写)是“类名”。在文档中,args*
是一个类似正则表达式的简写,表示可能需要零个或多个参数(前面的示例需要零个参数)。您还有其他想要完成的事情吗?
PS
“如有疑问,请询问编译器。”
在学习 Clojure 时,我也遇到过类似的问题,对文档和文档感到困惑。不完整或含糊不清的书籍。后来我才知道它要快得多,而且速度更快。尝试简单的实验来填补空白更有教育意义。
我认为简单的代码片段比 REPL 更容易/更快:
使用 我最喜欢的模板项目 ,结果
另外,
请仔细注意双引号的存在/不存在。
You are delving into internal details of Clojure here. This is normally not required (& usually not useful).
An expression like
(.toUpperCase "deepak")
calls the method "toUpperCase" on the java.lang.String object"deepak"
, egThe compiler translates the above internally into
which of course has the same result.
You can find more information in the docs for The Dot Special Form
The docs are sometimes hard to read, but you can decipher it with a little pattern matching. In the above example,
.toUpperCase
is ".instanceMember" and"deepak"
is the "instance" that is of typejava.lang.String
.For
(.getName String)
, the symbolString
(shorthand forjava.lang.String
) is the "Classname". In the docsargs*
is a regex-like shorthand for saying zero-or-more arguments may be needed (the previous examples have zero args required).Was there something else you wanted to accomplish?
P.S.
"When in doubt, ask the compiler."
I struggled with similar questions when I was learning Clojure, getting confused by docs & books that were incomplete or ambiguous. I later learned it was MUCH faster & much more educational to try simple experiments to fill in the blanks.
I think simple code snippets are easier/faster than the REPL:
using my favorite template project, with result
Also,
Note carefully the presence/absence of double-quotes.
文档字符串可能具有误导性 - 这可能是您感到困惑的原因。
有两种不同的方法可以使用“点”形式访问 Java 互操作。有 成员访问
(.toUpperCase "deepak")
,并且有 点特殊形式(."deepak" toUpperCase)
。您显示的文档字符串用于会员访问(.toUpperCase "deepak")
,而您查看的表单是点特殊形式(."deepak" toUpperCase)
。在大多数情况下,最好使用互操作的成员访问样式。在内部,互操作的成员访问样式被转换为点特殊形式(正如您通过执行macroexpand-1
发现的那样)。因此,在
(.toUpperCase "deepak")
的情况下,使用文档字符串进行成员访问(.instanceMember instance)
,对应关系为:instanceMember
code> 映射到toUpperCase
instance
映射到"deepak"
The docstring is potentially misleading - which may be the cause of your confusion.
There are two different ways to access Java interop with the "dot" form. There is Member access
(.toUpperCase "deepak")
, and there is the dot special form(. "deepak" toUpperCase)
. The docstring you showed is for Member access(.toUpperCase "deepak")
, whereas the form you were looking at is the dot special form(. "deepak" toUpperCase)
. It is preferable that you use the Member access style of interop in most cases. Internally, the Member access style of interop is translated into the dot special form (as you found by doing themacroexpand-1
).So, in the case of
(.toUpperCase "deepak")
, using the docstring for Member access(.instanceMember instance)
, the correspondences are:instanceMember
maps totoUpperCase
instance
maps to"deepak"