A Taste of Value Types
Quick tutorial on how to play yourself with the Project Valhalla Early-Access Builds and IntelliJ
A few days ago project Valhalla released a early-access build. I started to play around using command line tools, but when I tried to configure IntelliJ I’ve discovered that is much easier than I anticipated. So I’m sharing the instructions here in case they may be useful to someone else.
First of all, what is Project Valhalla and the L-World? It will be probably the major change coming to Java in the near future. A change as big as lambdas or module system: the introduction of Value Types.
This is the best description I found on what they are:
Value types are intended to be a third form of data type available in some future version of Java, to complement the currently-existing two: primitive types, and object references. The phrase most often used is that Java value types should “code like a class, work like an int”. This means that they should be a composite data type (code like a class) but lack identity and not pay the object header cost if at all possible (work like an int).
Ben Evans (@kittylyst)
There are a lot of resources on internet about it, and it is very exciting to be able to follow the project from the first discussions to the current day-to-day implementation details on mailing lists and talks.
But since we are bored by long explanations let’s go directly to the instructions.
First you have to visit the official site and download the J11 modified jdk:
Click on your architecture and download the tar.gz. I did the test on a linux box with ubuntu 16.04 as reference.
Once you download the tar.gz file you have to extract on the same directory with the other JDKs. It came with the nondescript name jdk-11 so I renamed it.
As you have done this you already did the most complex part, now you have only to open IntelliJ and create a new Java command line project. No need to setup JAVA_HOME or other env vars.
On project settings press new SDK button and select the directory you have just extracted. It should come as “11” in the combobox.
Then you select X-Experimental features as Project language level.
Now you can create your Value Types and use them from your project!
If you want, you can start copying my code here
This is the main:
And this is the Point Value Type:
Although the editor show the (temporary) new syntax in red it will compile and run without problems.
When declaring a Value Type you to specify the __ByValue modifier before the class name.
Note that to create a new Value Type instance you shouldn’t use the keyword new but __MakeDefault and __WithField to return a modified instance.
To run the code you have to remember to specify the -XX:+EnableValhalla VM option in this way:
Other possible options are listed here:
Now you start experimenting yourself, I will update my findings in this blog.
It is especially interesting to compare the memory allocation performance and how arrays works. In the future there will be also reified generics for ValueTypes.
So far there are two things I’m surprised: == doesn’t work on value types (I suppose it will eventually) and new is actually working (I suppose it won’t).
In any case is really great to be able to play around with this already!