Hacker News new | past | comments | ask | show | jobs | submit | _old_dude_'s comments login

In JavaScript, a 'let' inside the initializer of a for loop is captured by value, all the others are captured by reference.

I think it's fair to call that semantics "implicit".


No, that's a mistake in the article. The variable is still captured by reference, but `let` is causing it to be re-declared on every iteration of the loop, not mutated.

The following code prints 1, 2, 3. It wouldn't do that if the variable was captured by value.

    for (let i = 0; i < 3;) {
        setTimeout(() => console.log(i));
        i++;
    }


The behavior of "let" with for loops where the variable is declared more times than it is initialized, despite the source code having one declaration that is also the only initialization, is not very explicit.


Fair, but that's a property of for loops. Variable in closures are still always captured by reference.


consider this

   for (let i=0;i<3;i++) {
       i+=10;
       setTimeout(_=>console.log(i),30);
       i-=10
     }
Capture by value would print 10, 11, 12 that's the value when it was captured

Capture by reference would print 0,1,2

It's much easier to conceptualise it as

     for (const i=0;i<3;i++) {
      setTimeout(_=>console.log(i),30);
     }
which is fine because i never changes. It is a different i each time.

fancier example

    for (let x = 0, y = 0; y < 2; x=x++<3?x:y++,0){
        x+=10;
        y+=10;
        console.log("inline:",x,y);
        setTimeout(_=>console.log("timeout",x,y),30);
        x-=10;
        y-=10;
    }


> which is fine because i never changes. It is a different i each time.

This is clearly a super weird hack to make closure capture behave more like you'd want. There's a reason this level of weirdness isn't needed in C++.


Parroting something i have heard at a Java conference several years ago, tail recursion remove stack frames but the security model is based on stack frames, so it has to be a JVM optimization, not a compiler optimization.

I've no idea if this fact still holds when the security manager will be removed.


The security manager was removed (well, “permanently disabled”) in Java 24. As you note, the permissions available at any given point can depend on the permissions of the code on the stack, and TCO affects this. Removal of the SM thus removes one impediment to TCO.

However, there are other things still in the platform for which stack frames are significant. These are referred to as “caller sensitive” methods. An example is Class.forName(). This looks up the given name in the classloader of the class that contains the calling code. If the stack frames were shifted around by TCO, this might cause Class.forName() to use the wrong classloader.

No doubt there are ways to overcome this — the JVM does inlining after all — but there’s work to be done and problems to be solved.


Is there? As you say, there's already inlining, and I don't see how tco presents a harder case for that.


There are similarities in the problems, but there are also fundamental differences. With inlining, the JVM can always decide to deoptimize and back out the inlining without affecting the correctness of the result. But it can't do that with tail calls without exposting the program to a risk of StackOverflowError.

We've been using TCO here ("tail call optimization") but I recall Guy Steele advocating for calling this feature TCE ("elimination") because programs can rely on TCE for correctness.


I'm sympathetic to the points being made but the argument that Oracle does not have its own JavaScript runtime does not hold. An OracleBD is able to execute triggers written in JavaScript since quite some time.

see https://blogs.oracle.com/java/post/multilingual-engine-execu...


I don't think the article outright claims Oracle has no JavaScript runtime, only that Oracle JET is no runtime, which is true. And since this is the evidence Oracle presented to keep the trademark, it's fair to point out that this is nonsense. But it's also true that if this goes to court, Oracle could present GraalJS (which is used in OracleDB) as evidence for their case.


There is also the nashorn JavaScript engine and graaljs.


Is their a design document, somewhere about this new library ?

The jep does not explain how to prepare to new Java bytecodes and future enhancements. Am i suppose to only run with the latest version of Java if i want to use that library ?


> the default is trivial, it just returns the object's address

This was trivial a long time ago. Now, all Java GCs move objects in memory.


Very true, in Java, at least in the last 20 years, inheritance is de-facto deprecated, all new bits and bolts like enums, annotations, lambdas or records do not support inheritance.

So you have to use composition.


COBOL changes very slowly, once in a decade or two. Python does not offer support of a release for more than 3 years and a half [1].

[1] https://en.wikipedia.org/wiki/History_of_Python


But a compute instance or bare metal computer that never needs a new release wont have to deal with that in python either

Its only new builds on someone else’s computer that have this modern issue


I could believe there are legacy installations happily humming away on Python 2.7 without issue.


Several years ago I briefly worked at a major telecommunications provider with services across the southern United States that ran Python 2.4 on their production provisioning servers. Worked just fine.


The difference being that the COBOL is still supported after a decade.


ActiveState still offers a supported Python 2.7 version across all major platforms for those who need it (https://www.activestate.com/products/python/python-2-7/), so that's 14 years and counting.

If enough stuff needs it, people will keep it running. Java 8 will probably be in the same boat eventually if/when Oracle finally drops support.


I am not even sure what support is needed at this point. The interpreter is what it is. You know there are no new libraries to integrate.

I guess deploying it on a newer OS which might make it challenging to install unless it is a freshly compiled build?


Patches for security issues, most notably.


I'm using Java Almanac [1].

[1] https://javaalmanac.io/


BTW, Saint Louis is the common name of Louis IX not Louis XIV (the Sun King).

https://en.wikipedia.org/wiki/Louis_IX_of_France


Interesting, I didn't even realize that Saint Louis was one of the French kings; I guess I got lucky I didn't accidentally pick the same one twice!


This is similar to the expression tree API of C# so no change in the JVM is required, only cooperation between the java compiler and the reflection API.

The code model can be computed by the compiler, inserted in the classfile in a specific binary or text format and read at runtime using the reflection API.

I've not taken a look to the implementation so it's speculation but I do not think the JVM need to be changed.


Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: