无法理解点(`.`)特殊形式的参数

发布于 2025-01-15 03:48:21 字数 744 浏览 2 评论 0原文

在测试 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

罪#恶を代价 2025-01-22 03:48:21

您将在这里深入研究 Clojure 的内部细节。这通常不是必需的(并且通常没有用)。

(.toUpperCase "deepak") 这样的表达式调用 java.lang.String 对象 "deepak" 上的方法 "toUpperCase",例如

(.toUpperCase "deepak") => "DEEPAK"

编译器在内部将上面的内容翻译成

(. "deepak" toUpperCase) => "DEEPAK"

这当然有相同的结果。
您可以在 The Dot Special Form 的文档中找到更多信息

这些文档有时很难阅读,但是你可以通过一些模式匹配来破译它。在上面的示例中,.toUpperCase 是“.instanceMember”,“deepak”java.lang.String 类型的“instance” >。

对于(.getName String),符号Stringjava.lang.String的简写)是“类名”。在文档中,args* 是一个类似正则表达式的简写,表示可能需要零个或多个参数(前面的示例需要零个参数)。

您还有其他想要完成的事情吗?


PS

“如有疑问,请询问编译器。”

在学习 Clojure 时,我也遇到过类似的问题,对文档和文档感到困惑。不完整或含糊不清的书籍。后来我才知道它要快得多,而且速度更快。尝试简单的实验来填补空白更有教育意义。

我认为简单的代码片段比 REPL 更容易/更快:

(ns tst.demo.core
  (:use tupelo.core tupelo.test))

(dotest
  (spyx (.toUpperCase "deepak"))
  (spyx (. "deepak" toUpperCase))
  (spyx (type "deepak"))
  )

使用 我最喜欢的模板项目 ,结果

-----------------------------------
   Clojure 1.10.3    Java 17.0.1
-----------------------------------

Testing tst.demo.core
(.toUpperCase "deepak")    => "DEEPAK"
(. "deepak" toUpperCase).  => "DEEPAK"
(type "deepak")            => java.lang.String

另外,

(.getName String)             => "java.lang.String"
(.getName java.lang.String).  => "java.lang.String"

请仔细注意双引号的存在/不存在。

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", eg

(.toUpperCase "deepak") => "DEEPAK"

The compiler translates the above internally into

(. "deepak" toUpperCase) => "DEEPAK"

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 type java.lang.String.

For (.getName String), the symbol String (shorthand for java.lang.String) is the "Classname". In the docs args* 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:

(ns tst.demo.core
  (:use tupelo.core tupelo.test))

(dotest
  (spyx (.toUpperCase "deepak"))
  (spyx (. "deepak" toUpperCase))
  (spyx (type "deepak"))
  )

using my favorite template project, with result

-----------------------------------
   Clojure 1.10.3    Java 17.0.1
-----------------------------------

Testing tst.demo.core
(.toUpperCase "deepak")    => "DEEPAK"
(. "deepak" toUpperCase).  => "DEEPAK"
(type "deepak")            => java.lang.String

Also,

(.getName String)             => "java.lang.String"
(.getName java.lang.String).  => "java.lang.String"

Note carefully the presence/absence of double-quotes.

赏烟花じ飞满天 2025-01-22 03:48:21

文档字符串可能具有误导性 - 这可能是您感到困惑的原因。

有两种不同的方法可以使用“点”形式访问 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 the macroexpand-1).

So, in the case of (.toUpperCase "deepak"), using the docstring for Member access (.instanceMember instance), the correspondences are:

  • instanceMember maps to toUpperCase
  • instance maps to "deepak"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文