“let表达式”的目的是(LetExpr)在Java编译器中?
Java 编译器似乎支持 com.sun.tools.javac.tree.*
中的 let
表达式(查找 LetExpr
)。
JCTree 中的一条评论甚至提到了一些语法,
(let int x = 3; in x+2)
这些语法当然不被该语言的语法所接受,并且在早期编译器阶段被拒绝。
我想知道这个构造的起源,我以前从未见过。
它是由javac
内部使用的还是由其他工具合成的?它是否可能只是 Java 早期的一个从未出现过的语言特性的产物?
今天有什么有用的事情可以用它来做吗?
一般来说,它为什么存在?
The Java compiler seems to have support for let
expressions in com.sun.tools.javac.tree.*
(look for LetExpr
).
One comment in JCTree even mentions some syntax
(let int x = 3; in x+2)
which of course is not accepted by the grammar of the language and rejected in an earlier compiler phase.
I'm wondering about the origin of this construct, which I have never seen before.
Is it used internally by javac
or is it synthesized by other tools? Is it maybe just an artifact from the very early days of Java from a language feature which never saw the light?
Is there anything useful which can be done with it today?
Generally speaking, why does it exist?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如谷歌建议的那样,它的存在是为了自动装箱。
如果您有这样的代码:
Java 在内部将其放入此辅助表达式中:
来源:https: //bugs.java.com/bugdatabase/view_bug?bug_id=6614974
该表达式显然没有语法表示,它只是一个 AST 级别的转换,以简化编译。
It exists for autoboxing as Google suggests.
If you have code like this:
Java internally makes this into this helper expression:
Source: https://bugs.java.com/bugdatabase/view_bug?bug_id=6614974
That expression obviously has no syntax representation, it's just an AST level transformation to simplify compilation.
这称为 let 形式,用于“缩写”。
另一方面,在过程语言中,这称为“声明变量”,因为变量的“值”单元格可以在过程语言中发生变化。 (在函数式语言中,它只是一个缩写,与一开始就写出来没有什么不同)
我可以想到很多在用户编写的源代码中使用它的语言(Haskell、ML、Scheme、SBCL、 Arc,...),所以不确定你怎么还没有看到它...
或者你的意思只是在Java中?
是:的简写形式
,最终会被简化为
\
应该是 lambda 的位置。至于为什么是Java,不太清楚。它应该做的是声明变量,所以检查它是否在那里使用。
This is called the let form and is used for "abbreviating".
On the other hand, in procedural languages this is called "declaring a variable" because the variable's "value" cell can mutate in procedure languages. (In functional languages, it's just an abbreviation and no different to just writing it out in the first place)
I can think of a lot of languages that use it in the source code the language user writes (Haskell, ML, Scheme, SBCL, Arc, ...), so not sure how you didn't see it yet...
Or did you mean just in Java?
Is shorthand for:
which will eventually be reduced to
where
\
is supposed to be lambda.As for why it's in Java, not sure. What it's supposed to do is declare variables, so check whether it's used there.