The purpose of the "let expression" (LetExpr) in the Java compiler?

The Java compiler seems to support let expressions in com.sun.tools.javac.tree.* (Find LetExpr ).

One JCTree comment 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 the earlier phase of the compiler.

I'm curious about the origin of this design, which I have never seen before.

Is it used inside javac or synthesized by other tools? Maybe it's just an artifact from the earliest days of Java from a language function that never saw the light of day?

Is there anything useful you can do with it today?

Generally speaking, why does it exist?

+70
java compiler-construction let language-design
Dec 04 2018-11-12T00:
source share
2 answers

Generally speaking, why does it exist?

It exists for autoboxing, as Google suggests.

If you have a code like this:

 Integer foo = 0; foo++; 

Java internally does this in this helper expression:

 Integer foo = 0; let int foo_helper = foo.intValue() in foo_helper++; 

Source: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6614974

This expression obviously does not have a syntactic representation; it is simply a conversion of the AST level to simplify compilation.

+43
Dec 04 '11 at 1:26
source share

This is called the let form and is used for "abbreviating".

On the other hand, in procedural languages ​​this is called a "variable declaration" because the cell of the variable "value" can mutate in the procedure languages. (In functional languages, it's just an abbreviation and is no different from just writing it first)

I can think of many languages ​​that use it in the source code written by the user of the language (Haskell, ML, Scheme, SBCL, Arc, ...), so I'm not sure how you have not seen it ...

Or did you mean only in Java?

 let x = 2 in (x + 5) 

Abbreviation for:

 (\x (x + 5)) 2 

which will ultimately be reduced to

 (2 + 5) 

where \ assumed lambda.

As for why this is in Java, not sure. What he should do is declare variables, so check if it is used there.

+3
Dec 04 2018-11-12T00:
source share



All Articles