JVM

Will Valhalla bring better nulls to Java? – JVM Weekly vol. 124

Three topics today, and actually each of them deserved a headline… Which, however, shows how much has been going on in the past week. So there will be the announcement of Kotlin 2.0, a set of experiments with Project Loom, and…. could it be that Kotlin Nulls are about to appear in Java?

Article cover

1. Kotlin 2.0 is coming

It turns out that a lot of changes are coming to the world of Kotlin – at least when it comes to versioning.

Indeed, the developers of have announced that the 1.9 release will be the last of the 1.x line. Version 1.10 will not be released, instead, the whole project will be jumping straight to the 2.0 release. This is due to this version is going to bring the long-awaited K2 compiler  – “one to rule them all” and is intended to provide a common infrastructure for all potential language targets. This will ensure that Kotlin creators won’t have to implement the same compiler functionality for JVM, WebAssembly, or Android each time, which is expected to significantly speed up language evolution. Thus, the change is so big that it was deemed reasonable to properly crown it with a bump in versioning.

The change of a “major” version of a language has been able to heavily mess up the ecosystem of many languages, but in the case of Kotlin, JetBrains promises a very stable migration process. This is to be achieved through two components. First, the changes motivating the version bumping are taking place under the hood, and the developers deliberately do not plan to introduce any new innovations in the language’s syntax in the new release. They are saving those for the future 2.x releases, which will come after the successful transition to K2. In addition, however, JetBrains benefits from the fact that it controls both Kotlin language and is the main supplier of tools for it. This allows the whole operation to run much more smoothly.

Vertical Integration, a bit like Apple.

Simultaneously with the above announcement, a preview version of the 1.8.20 release was released, which introduces the -language-version 2.0 flag. This one allows you to test the latest changes in the compiler. What’s in them? We’ll probably return to the topic with the release of the stable 1.8.20, but if nothing changes it will be a release heavily focused on the internals of the language.

Yet Roman Elizarov is already communicating that as soon as they release Kotlin 2.0 and K2 they will move with a kick.

Sources

Discover more IT content selected for you
In Vived, you will find articles handpicked by devs. Download the app and read the good stuff!

phone newsletter image

2. MicroProfile, Scala and Kotlin communities have fun with the Loom project

Some Arount-The-Loom announcements have accumulated so we will devote the whole section to the topic.

Let’s start with Helidon Níma, for the project released the ALPHA4 version last week. I’m not used to informing you about such an early stage of framework development, but this time Alpha is really interesting. This is because the developers of Níma have managed to make quite an achievement, which is the creation of a MicroProfile 5.0 API implementation compatible with Virtual Threads, specifically with the WebServer implementation provided by Níma. This is a huge leap for the project (and actually for the entire community around MP).

Last week also saw the appearance of Prototype Loom-based concurrency API for Scala Adam Warski, CTO of SoftwareMill. He experiments with the concept of creating a “native” API for virtual threads in Scala, using the language’s type system in a way that prevents programmers from kicking themselves in the knee. After all, keep in mind that virtual threads themselves are a fairly low-level API that most programmers will probably use through some abstraction, however.

The text touches on Structured Concurrency, Scoped Values, and makes a comparison of the experimental API (a prototype of which Adam called ox – cOncurrency eXtensions) with existing solutions in Scala. At this stage, as one can easily guess, the libraries that have been under development for many years are much more mature. However, it should be remembered that, as is the case with Reactive Extension, functional code is simply more difficult to understand for the untrained programmer. What Loom, on the other hand, brings is the simplicity of usage, even if its abstraction does not yet have ready-made patterns for all problems.

The text itself is great, though, and if you have even a passing interest in functional languages, this will be a great read. The Ox code itself is also available on GitHub, perfect for those who prefer to learn by hands-on experimenting.

And since we have already touched Kotlin today, let’s also take a look at how this language is preparing for the upcoming Project Loom launch. I’ll admit, that browsing through the language’s roadmap one looks in vain for virtual threads there, but this may be due to the fact that coroutines themselves are flexible enough to run on multiple Dispatchers. This means that you can easily “swap out” the asynchronicity model running underneath. So just as we have a Dispatcher for RX, it is easy to create one for Loom.

Me, for a moment before kicking myself in the knee and using my own Coroutine Dispatcher in production.

The topic resurfaced in December in JetBrains’ “Talking Kotlin” podcast, where Urs Peter elaborates on it and, despite the ominous title Will Loom Kill Kotlin Coroutines? builds a narrative that in fact, Kotlin will be the first to consume value from virtual threads. After all, we already mentioned that they are low-level feature, and Coroutines already provide high-level APIs with standardized ways to deal with asynchronous programming – so this is the same theme that runs through the above article on Scala. If you’re curious about what advantages Project Loom will have for Kotlin programmers, an article kt.academy appeared Running Kotlin coroutines on Project Loom’s virtual threads, showing how you can already play with the whole thing today.

Because let’s be honest, anyone who has dabbled a bit in bytecode realizes that in fact, Kotlin’s had to hack the lack of Coroutine in Java hard – as you can see in the above example.

Sources

3. Is Valhalla going to bring better nullibility support to Java?

Finally, we will deal with Valhalla. After a period of silence, the project has started publishing updates again. The latest one is so interesting that, despite the very early stage of discussion, it is worth briefly mentioning here.

One of the features of the language that Valhalla is supposed to provide are Value Types (or whatever they’re called in the current iteration). One obvious distinction between “reference types” (i.e., those currently existing in the language) and “value types” is that the latter cannot take null values. Unlike Kotlin, this property is not easily expressed in the language itself. That’s why designers for Valhalla are considering introducing a new “nullness” marker for objects.

Because as you can see, the difference is considerable

The new feature is supposed to allow objects to clearly mark whether they allow nullability or not, and keep this information in the runtime. This will help the JVM optimize how they are stored in the heap. For now, there is no proposal for a specific syntax; for simplicity, the developers are using a Kotlin-like syntax in correspondence (Foo! as non-null Foo and Foo? for a variant of Foo or null). However, these are still very early days and the JVM team does not rule out using some kind of annotation like @NonNull for this purpose indstead.

And – as we’ve come up with a reference to Kotlin in every other section – it is worth mentioning that other languages using Java as Host Language can only benefit from this change. After all, the developers of the JVM intend to use this marker for better memory management at the level of the virtual machine itself.

The tide lifts all boats.

A valuable discussion about the proposal can be found on Reddit

Sources

Discover more IT content selected for you
In Vived, you will find articles handpicked by devs. Download the app and read the good stuff!

phone newsletter image

Bonus: A report The State of Spring 2022 has been released.

I ran across the report, and there is unlikely you will find anything truly revolutionary in it, but I will share with you two facts that caught my eye.

First of all, 35% of Spring users say they are using a reactive version of Spring. It is also interesting to note the growth (though not a huge one) of Spring’s use in Serverless solutions.

The Spring community (at least those who have heard of them) also reacts enthusiastically to new developments in Java. 90% who are aware of Loom would like to be able to use it in Spring, and GraalVM is on the radar as much as 98%. The enthusiasm is there, and it will be interesting to see how many will actually succeed in getting native images into production.