Do you dream of "value types" in Java? So do I! Hence I was thrilled to see that Project Valhalla is slowly coming out of exploration with two draft JEPs.
Here's what they currently propose. 🧵👇🏾
(If you prefer video: https:/www.youtube.com/watch?v=WBvTilbh8S0&t=344s)
1/10
First, why value types? To bridge the divide between
* primitives (fast, no memory overhead) and
* reference types (abstraction, safety, identity)
As is, we sometimes have to choose between performance and design. And we often choose wrongly.
* for reference types
* called "identity classes"
* instances are "identity objects"
For identity classes / reference types everything stays as is.
3/10
2. Interface `PrimitiveObject` (the exciting part):
* for a new kind of type
* called "primitive classes"
* instances are "primitive objects"
"JVMs may freely duplicate and re-encode them in an effort to improve computation time, memory footprint, and garbage collection"
4/10
Primitive classes want to make good on Valhalla's promise:
"Codes like a class, works like an int."
See strawman code attached. Details:
* class and fields are implicitly final
* no circular dependency on itself
* no synchronized methods
* more in the JEP
5/10
A bit more terminology on primitive classes:
* `Point p` is a "primitive value"
* `Point.ref p` is a "primitive reference"
Primitive values will be like today's primitives (no `null`, no memory indirection/overhead), primitive references like their wrapper types.
6/10
But this doesn't cleanly map to today's situation because if `Integer` were a primitive class, `Integer i` would be a non-nullable primitive value and `Integer.ref i` the primitive reference. To fix that mismatch, the JEP proposes "reference-favoring primitive classes"…
7/10
… A primitive class can be declared as "reference-favoring". If that were the case for `Point`:
* `Point.val p` is a primitive value
* `Point p` is a primitive reference
Compared to earlier, it's flipped - now references are the default and values need special syntax.
8/10
Got all that? Great! 👍🏾 Because the 2nd draft JEP openjdk.java.net/jeps/8259731 proposes to put it into practice on today's primitives:
1. Turn wrapper classes into reference-favoring primitive classes. 2. Treat primitive type keywords as aliases for their primitive value types.
9/10
That's it for those two draft JEPs. Hope you're as excited as I am, but keep in mind that everything can still change.
FAQ:
* What about generics? ~> A separate draft JEP is coming up.
* When will this be released? ~> Your guess is as good as mine, which is "not 2021".
10/10
• • •
Missing some Tweet in this thread? You can try to
force a refresh
"When people ask me what feature do I most regret, serialization is the easy answer" - @BrianGoetz
Let's talk about serialization in Java: Why does it exist? What are the problems (even today)? Would Java have been better off without it? Can it be fixed?
1/11
NB: What follows is mostly quotes or paraphrasing from a conversation I had with Brian during my 25-hour live stream. If you want to watch the full discussion about serialization, nullability, primitives, and more, you can find it here:
2/11
Serialization was introduced because turning an object graph into bytes is valuable: You can store things on disk, in databases, or send them over the wire. But while the concept is sound, it was implemented in a horrible way.
Project Amber is making progress on pattern matching in #JavaNext. Here are three recent developments that I'm very excited about and I think you will be as well.
(Caveats: these are ongoing discussions; none of this is final; speculation and strawman syntax ahead)
1. "Array patterns" allow matching and destructuring arrays. The `if`-line does three things:
a) is `objects` a `String` array with length 2?
b) if so, cast it to `String[]` and extract the two elements
c) declare `a` and `b` and assign the two elements to them
"Can I also bind the arrays as a whole?"
Likely. This is called an "as pattern".
"What if the array can have more elements?"
Allowing to express "at least two elements" is being considered - for example with three dots.
Yesterday evening, we spent about two hours digging through the German #Corona app and I'm thoroughly impressed. This is a modern project, developed out there as free software (APL 2.0) on GitHub, and it has stellar documentation.
Android and iOS apps use Google's/Apple's Exposure Notification Framework (ENF; google.com/covid19/exposu…, apple.com/covid19/contac…). Android app is written in Kotlin, iOS app in Swift. (Can't tell you much more because I'm clueless about mobile.)
The electoral swing analysis ("Wählerwanderung") of yesterday's state elections in Germany (e.g. @diezeit, zeit.de/politik/deutsc…) is pretty interesting and I want to comment on who switched to the radical right-wing #AfD.
🖗 Thread
All numbers are relative swing from [voters of $party in 2014 who voted in 2019] to [voters of AfD in 2019] for Sachsen / Brandenburg, i.e. "the usual numbers" normalized by 2019 voters.
In order of their share lost to #AfD, here are the big parties and then former non-voters...
It's pretty obvious that there's no arguing with extremists. Retaining ~90% of voters from one election to the next is "impressive", even for the traditionally stubborn right part of the political spectrum.
I just got my (electronic) copy of #EffectiveJava3E and am reading the new parts. Will tweet a little about it...
Nothing spectacular going on with Optional (of course I started with that, what did you think?!) until close to the end. "Optional fields are a smell, but here's an example, where it's justified".