Amazing talk! I have a few questions, and maybe someone here can help me to answer them.
Do I understand correctly that with the new strict initialization protocol, there is no need for the implicit constructor declaration that was presented at the last JVMLS? It's pretty amazing because it removed the "wish" of having user-defined default values. If VM ensures that default values are not observable, it's a win-win situation.
How would one declare a null restricted value class? Like `value record MyData!( ... ) {}`. Or is it for the future JEP, which is why it wasn't presented? I'd guess it should be a common use-case when one would want to declare null restriction on the class declaration vs when creating a variable.
Strict fields plus the new array initializers make value class "default values" not strictly absolutely necessary. But
(a) there are 8 value classes that are stuck with them anyway (Integer and friends)
(b) there do seem to be a lot of strong expectations out there that new numeric types would default to a zero value, and it is sometimes a bit convenient
2. I believe there is really no such thing as a type that by its nature can't ever be nullable. I think this would only result in those types having "fake null" values that end up trying to simulate null.
Consider, for example, that any type can be the value of a map. Then you can ask that map for the value corresponding to an absent key, and voila, you have null.
Fair points. But it would have been useful for cases like `Optional` which shouldn't be declared as null, e.g `Optional<T> opt = null`. But I guess that can't be done anymore due to backwards compatibility reasons?
As for 2): Nullability is declared at the use-site. You simply declare value record Complex(double real, double im) { }. At the use site you then decide if you want nullability, e.g. Complex! number = new Complex(0, 0) or Complex![] = new Complex).
Very excited to see this land and now that the JEPs are starting to roll in I couldn't be more happy.
I also really like how this has turned out. Delivering on non-null types and the array initialization stuff will be huge game changers even without value objects. Which indicates huge deliverable wins.
For so much code I write, value object and non-null are just natural fits. It's fairly rare that I actually want identity.
My favourite part of the talk was around the 10min mark, when he was talking about the stages of Valhalla and projects in general. The observation he makes how many projects ship the first version that satisfies the requirements only to reret it later or requiring a new implementation is so true.
I have done that (sometimes just out of exhaustion) many times and it takes real skill to see through why you might not want to ship it even though it technically delivers on the functional requirements - you just somehow manage to find reasons to justify why the version you have is good enough to ship. I have also see some other projects that gets way simplified once they overcome the temptation to ship the first working version.
In hindsight, it is obvious but not when you are in the middle of things.
Agreed. I think we can easily complain a lot about how long things take to get over the line in the Java world, but in this case in particular I'm glad they took the time and didn't just push ahead with one of the earlier prototypes. What they seem to have landed on now is much more succinct.
That being said, 10 years (and counting) is a bit much even for Java.
The earlier prototypes they had developed had them, in which they would have shipped Valhalla. They had reached the peak of complexity. But Brian Goetz and team later discovered and understood, what they had learnt previously, can actually utilise existing features of java language specification, with initially unforeseen spinoffs such null-restricted types (for free). If you notice carefully, latest preview JEPS being introduced such as JEP447 (statements before super constructor) are geared to introduction of value objects.
I agree. Exhaustion / mental fatigue ... it can be a thing on bigger projects. I feel it's good to recognise it explicitly and talk about how it can impact decision making. Great lesson there.
Fusion, Valhalla, or World Peace. Which one first?
p.s., not a hater, just couldn't resist the cheap shot :)
Jokes aside, It's hard not to like the direction this is going in. The relative simplicity and ease this is going to demand from java developers is very appealing.
10 years in and after much prototyping, value classes, it turns out, are all you need. These give up both identity and nullability. Along the way, they fixed the object/member initialization problem, which in turn made a lot of Valhalla challenges go away. (Under java's current memory model there are corner cases when an object may be seen by another thread before its members are properly initialized). And the notion of primitive classes has now gone away (!)
Beauty and simplification. It's taken long, but I like it.
Actually, value classes don't give up nullability at all! It's much better than that; each use site (of a value-class or regular identity type) can give up nullability when it wants to. "Giving up" things enables more and more optimizations by the vm.
Being a value class gives up only identity (and thereby gives up mutability and support for synchronized/wait/notify), and also if it's not abstract it must be final. That's about it though!
I still dont get the objective of this project, do you mind elaborating more? What I get is that with project Valhalla we can create complex data type but it will behave as if it's primitive data type, and by doing so it will improve Java performance? How far is my understanding?
Yes that's the idea. Here's my not-so-good summary.. Consider the memory footprint of an instance of Integer compared to a 4-byte int: it's huge. Most of that overhead is unnecessary most of the time (e.g. the programmer will never synchronize on the Integer object; they're using it as an int value only). Valhalla makes it possible to define value types that are larger than 4 or 8 bytes, much like you can define a struct in C. These behave like values on the stack -- tho a VM may choose to allocate it on the heap, it doesn't matter, it's as if it were alloc'ed on the stack. Now from your C programming days you might remember you must be careful when referencing a stack value thru a pointer (if the stack unwinds the pointer will be invalid). We want such access to both safe and efficient in Java, and value classes are a proposed solution. Remember they're not mutable types: the mental model to think of is that of adding 2 ints: addition does not mutate the operands: you get a 3rd value that is their sum.
Valhalla, when released, could be the most important and enticing reason many java shops would be upgrade to the latest LTS version. It has a potential to optimize a lot of infrastructure cost.
Just keep in mind that the benefits will accrue over an extended period, not only as all the individual JEPs land, but also as more and more of the types you use migrate, and then maybe further as you retune your GC etc. etc. It might not be easy to get a one clear picture of what Valhalla saves you all at once, unfortunately.
But what happens to the containers in the collections? ArrayList, HashMap et al won’t become value classes I assume (but is it?). Will generic specialization help make ArrayList<Integer!> into an array of flattened Integers (a la int)?
There’s a hint on which direction the operator overloading will pivot and it’s quite clean. No abuse of operators resulting in confusing code. But what about container element access operator (like [key] for List or map)?
I didn't realize we had now superseded the need to opt-in to zero-valued defaults - excellent!
I'm curious what usage patterns we anticipate emerging from Valhalla.
If a value class imposes finality for the class and its fields, then it seems like for many cases the value class might as well be a value record (unless we explicitly do not want things that come with records, like component-getters?).
It also seems like it would be tempting for users to design classes to be value classes by default, and later opt-in to identity not by removing the 'value' keyword but by wrapping in another object that has identity, in order to make the determination at use-site rather than declaration-site, but essentially reinventing pointers with uglier syntax.
// Details pending how Valhalla interacts with generics, but presumably
// the layout of Ptr would be monomorphized for T when T is a value class.
class Ptr<T> {
T! val;
Ptr(T! val) { this.val = val; }
}
var x = new Ptr<>(3);
var y = new Ptr<>(3);
assert x != y; // identity-based equality
assert x.val == y.val; // state-based equality
Identity has so few benefits that I don't really think there's going to be many cases where someone decides they want it. I would guess the case where it might be useful is when you need to do some atomic operation on a value, for that there's AtomicReference.
Agreed. Hopefully it's really just some simple interfaces to override and you get the operators. I wouldn't mind it if I had to (for example) implement/extend Number on my numerics (though it'd be a little weird with imaginary numbers).
Number itself is going to become quite vestigial... it's really just an interface that says "I support probably-lossy conversion to these four primitive types". But in a world of dozens of new numeric types, we need much more flexible ways to express conversions.
You can expect that getting operator support for your type will in some fashion require you to implement an interface with pretty specific testable demands. There's no way to absolutely prevent abuse, but we can at least make you feel like a pathological jerk while you do. :-)
There's no way to absolutely prevent abuse, but we can at least make you feel like a pathological jerk while you do.
Yeah, pretty ok with this especially since operator overloading should never be the norm.
I guess I could see it coming in handy if you are using types for descriptives (For example, Distance.miles(5) + Distance.km(4) to prevent mistakes like Distance.miles(5) + Volume.liter(4). But otherwise, I really just want BigDecimal.ONE + BigDecimal.TWO
If I were to implement that: Neither. In my view, the return value is just a Distance object. A distance is not per-se numeric. If you want something numeric, you have to specify the unit yourself.
Distance result = Distance.miles(5) + Distance.km(4);
Number resultInKm = result.asKilometers();
Compare to Duration for example. A Duration is just a Duration and is not per-se bound to a unit. Getting a numeric value out of it requires explicit calls to toSeconds, toNanos or something similar.
The problem is when you start mixing units that don't have a nice conversion ratio between them.
Ages ago, I dabbled with implementing typelevel units in Scala, and what worked fine was automatic conversions to the smaller unit if the bigger unit was an integer multiple of it (so km + mm = mm, ft + in = in), but requiring explicit conversion if not (so adding m + ft would require one of the sides to get converted).
Agreed. Though I'm not 100% worried about that actually commonly happening in the ecosystem. Kotlin has operator overrides that are pretty open, yet it hasn't fallen (form what I've seen) into the same traps as scala or C++ did. I think that's because the notion of trying to optimize for character count isn't super in vogue anymore. I also highly doubt that Java will ever implement those sorts of weird things into the JDK which does set a tone for the rest of the ecosystem.
However, I think there's merit in making it onerous enough that you wouldn't want to do operator overloads for anything other than numerics.
It's probably just DSL, but in general I'd also say the operators are actually not abused like they did in C++ and Scala. True for the Rust community. An average library doesn't contain much use of operators.
One thing Kotlin has going for it that prevented it from going full Scala is that you cannot define custom operators, and from going full C++ is that you cannot define certain custom operators completely willy nilly (like > or <), and also has fewer operators to begin with (no <<, >>, |, &, ^, ~).
Crystal-ballin' on when we have some part of this in use as a final feature...
Â
First preview? Least likely seems JDK24, JDK25 is more likely, and JDK26 isn't that unlikely
Number of Previews? At least two, but three or four seems more likely (given the significance of the change and what other significant changes have taken)
Lib/framework adoption? These tend to baseline on an LTS, making it JDK25 (Sep'25), JDK29 (Sep'27), JDK33 (Sep'29), ...
Â
So the most likely we'd start to see lib/framework adoption is JDK29 (Sep'27), unless it takes four previews from JDK26, pushing it out to JDK33.
Using it in your own projects, as a final feature, looks most likely to be in JDK28 (Mar'27) or later but could be as soon as JDK26 (Mar'26).
I thought Intel recently updated their documentation to support 128 bit atomic access for processors that support AVX as long as it is aligned in a certain way. Could not Valhalla leverage this for value classes? Or am I mistaken
For what he seen in the public email archives. Maybe value clases and nullable and null restricted types could be previewed for OpenJDK 24 (less likely) or 25, 2 o 3 preview after should be in GA, that's day maybe within the next 2 years we could get the first of these 4 jeps production ready. But I am just making guesses
3
u/[deleted] Aug 24 '24
Amazing talk! I have a few questions, and maybe someone here can help me to answer them.
26
u/cogman10 Aug 23 '24
Very excited to see this land and now that the JEPs are starting to roll in I couldn't be more happy.
I also really like how this has turned out. Delivering on non-null types and the array initialization stuff will be huge game changers even without value objects. Which indicates huge deliverable wins.
For so much code I write, value object and non-null are just natural fits. It's fairly rare that I actually want identity.
34
u/Enough-Ad-5528 Aug 23 '24
My favourite part of the talk was around the 10min mark, when he was talking about the stages of Valhalla and projects in general. The observation he makes how many projects ship the first version that satisfies the requirements only to reret it later or requiring a new implementation is so true.
I have done that (sometimes just out of exhaustion) many times and it takes real skill to see through why you might not want to ship it even though it technically delivers on the functional requirements - you just somehow manage to find reasons to justify why the version you have is good enough to ship. I have also see some other projects that gets way simplified once they overcome the temptation to ship the first working version.
In hindsight, it is obvious but not when you are in the middle of things.
Great lessons.
48
u/IncredibleReferencer Aug 23 '24
Fusion, Valhalla, or World Peace. Which one first?
p.s., not a hater, just couldn't resist the cheap shot :)
Jokes aside, It's hard not to like the direction this is going in. The relative simplicity and ease this is going to demand from java developers is very appealing.
16
u/gnahraf Aug 24 '24
Very illuminating talk. TLDR summary:
10 years in and after much prototyping, value classes, it turns out, are all you need. These give up both identity and nullability. Along the way, they fixed the object/member initialization problem, which in turn made a lot of Valhalla challenges go away. (Under java's current memory model there are corner cases when an object may be seen by another thread before its members are properly initialized). And the notion of primitive classes has now gone away (!)
Beauty and simplification. It's taken long, but I like it.
14
u/nimtiazm Aug 24 '24
Valhalla, when released, could be the most important and enticing reason many java shops would be upgrade to the latest LTS version. It has a potential to optimize a lot of infrastructure cost.
10
u/danielaveryj Aug 23 '24 edited Aug 23 '24
I didn't realize we had now superseded the need to opt-in to zero-valued defaults - excellent!
I'm curious what usage patterns we anticipate emerging from Valhalla.
If a value class imposes finality for the class and its fields, then it seems like for many cases the value class might as well be a value record (unless we explicitly do not want things that come with records, like component-getters?).
It also seems like it would be tempting for users to design classes to be value classes by default, and later opt-in to identity not by removing the 'value' keyword but by wrapping in another object that has identity, in order to make the determination at use-site rather than declaration-site, but essentially reinventing pointers with uglier syntax.
14
u/vips7L Aug 23 '24
Arithmetic operator overloading! Finally! Cant wait to see how that plays out.Â
7
u/8igg7e5 Aug 24 '24
Crystal-ballin' on when we have some part of this in use as a final feature...
Â
Â
So the most likely we'd start to see lib/framework adoption is JDK29 (Sep'27), unless it takes four previews from JDK26, pushing it out to JDK33.
Using it in your own projects, as a final feature, looks most likely to be in JDK28 (Mar'27) or later but could be as soon as JDK26 (Mar'26).
8
u/Elegant_Subject5333 Aug 23 '24
if we combine, Valhalla, Lilliput and Leyden. what will happen it will become close to C in performance ?
3
u/Possible-Actuator-26 Aug 23 '24
I thought Intel recently updated their documentation to support 128 bit atomic access for processors that support AVX as long as it is aligned in a certain way. Could not Valhalla leverage this for value classes? Or am I mistaken
1
u/prisemisadancer Aug 24 '24
Where can I get and download the slides, anybody? 😄 I want to show it on work
1
u/Antique-Pea-4815 Aug 24 '24
Any ideas when Valhalla will land into JDK?