10 ms·
State of Valhalla
- larsrc 5y agoNot run over by the ice giants yet.
- bluejekyll 5y ago“… it has the more ambitious agenda to heal the rift between primitives and objects.” I have been waiting for these fixes to the language since I was first introduced to Rust which got the type system “correct” where Java had left a lot to be desired. The big thing that Java will get beyond generics and such being cleaned up for all types, is true Optional types. That is, a stack based Optional that is itself not null, but tracks something that might be null in the interior. It will take a long time to update the ecosystem to correct this, but when it comes it will make Java a much “safer” language to develop in, that is a class of runtime errors can be avoided and caught at compile time. This will be great, and I look forward to the day when it comes.
- kaba0 5y agoDue to backwards compatibility Optionals will be value classes, not primitives so null will be a possible value for them as far as I know. But it will be possible to optimize it at many places (mostly as return types perhaps).
- bluejekyll 5y agoI’d be interested in a new type in that case. Option…
- Twisol 5y agoIf I understand the Valhalla proposed design [0], every primitive reference type `X` will have an associated primitive value type `X.val`, while every primitive value type `X` will have an associated primitive reference type `X.ref`. Which one gets to be called `X` is a function of how you declare it -- `primitive class X.val {}` gives primacy to the reference type. So, if `Optional` will continue to be a reference type, you should be able to use `Optional.val` to use the associated value type. [0] https://openjdk.java.net/jeps/401 https://openjdk.java.net/jeps/401
- _old_dude_ 5y agoI believe JEP 401 has not yet been updated to the new model, there is no .val anymore, instead there are nullable value types and non nullable primitive types, only the latter has a .ref box because the former is a reference. Optional will be retrofitted to a value class not a primitive class, you get flattening but only on stack (parameter type or return type) not on heap (field). I don't really understand the end of the part3 (the VM model), so i may be wrong.
- fnord77 5y agoa null Optional will be pretty disappointing
- syspec 5y agoOptionull
- stepbeek 5y agoHave you ever tried kotlin? I've found it to be an encoding of best practices from Java with nullability pinned down. Really made me enjoy working in a most OO/procedural language again after fleeing to scala for a few years.
- bluejekyll 5y agoYes. Kotlin is good, but there is a lot of Java out there that needs to be supported. Have you experimented with converting entire projects of Java to Kotlin?
- The_rationalist 5y ago
- halpert 5y agoWhy would you convert the entire project to Kotlin? It’s trivial to mix the two languages, and in fact most Kotlin applications depend on Java libraries.
- pjmlp 5y agoJet Brains seems to want ot create an ecosystem of their own, Kotlin still depends on Java is more accurate.
- halpert 5y agoI don’t think that’s true. One of the biggest use cases for Kotlin is Android development. Interop between Kotlin, Java libraries, and the Android SDK is completely seamless.
- 62951413 5y agoIt's hard to be certain but we have seen this story in Scala. Among the advantages advertised early was interoperability with Java and access to its huge ecosystem. In practice the Scala ecosystem is completely different (well, Akka is a special case popular in Java projects too). As a trivial example people would rather use ScalaPB instead of officially supported Java library. When I worked on a Kotlin-based system people were adamant to use github.com/JetBrains/Exposed. Which had a couple pages of documentation and was missing features. And then again there's politics. Big/successful companies always end up re-inventing PL wheels. The whole GOOG/ORCL mess gave GOOG incentive to fragment the Java ecosystem too.
- Matthias247 5y agoThe main thing of Rust that Java can’t provide even after Valhalla is compile time thread safety guarantees. I find those in the meantime one of the biggest advantages of using Rust - and miss it in all other programming languages. Questions like „can I refactor this code to make it parallel und use the database client / IO stream / etc from multiple threads simply answer themselves. Apartment from that I think the type system power of Java/Kotlin in the near future is good enough and a nice blend of complexity and getting things done. And with Valhalla, Loom and modern GCs a lot of performance concerns should also be removed
- kaba0 5y agoRust can only guarantee that no data race will happen. Other race conditions are still possible, so I don’t really see Rust that much safer than Java. Concurrency is hard everywhere.
- charleslmunger 5y agoIndeed. I recommend using tsan for java. https://wiki.openjdk.java.net/display/tsan/Main https://wiki.openjdk.java.net/display/tsan/Main But the types of races I struggle with in java I suspect are similarly hard in Rust. Here's two pieces of concurrent code I struggled with: https://github.com/google/guava/blob/master/guava/src/com/google/common/util/concurrent/ExecutionSequencer.java#L286 https://github.com/google/guava/blob/master/guava/src/com/go... https://github.com/google/guava/blob/master/guava/src/com/google/common/util/concurrent/SequentialExecutor.java#L162 https://github.com/google/guava/blob/master/guava/src/com/go... I'm not sure reasoning about the correctness of those would be easier in Rust, but if there are any rust experts reading this I'd love your perspective.
- kaba0 5y agoThanks, I haven’t seen tsan before. Java also has JML but I don’t know how much does it help regarding concurrency.
- Matthias247 5y ago
- boroboro4 5y agoIt's sad they introduced two kind of classes: "value" and these "kinds" together with both classes and records, java becomes much more complicated IMO.
- kaba0 5y agoI think these are different semantic axes. There is one axis of less and less safety guarantees (identity, value, primitive) while records are more of a “here is a short syntax if you promise to keep some conventions”. But as mentioned in probably the second part, `value record` will also work properly.
- boroboro4 5y agoWhile what you're saying true I'm pretty sure it's going to create a lot of confusion - these things share a lot - all of them have all the fields final for example, and feel like "simple" "data" classes. I can see their reasoning to add value classes in addition to primitive classes, but the difference between them is going to create the most confusion, since they are identical in terms of requirements to classes themselves.
- kaba0 5y agoIt will definitely give some additional learning to people, but I feel like their usage is still quite idiomatic. If someone didn’t even hear about these, he/she can still code as if they are identity-based classes for the most time (chances are they are not the one writing vanilla concurrent code with synchronized or using some advanced optimizations with identityHashCode), and conversely spamming value and primitive may fail at compile time or at worse, runtime with some meaningful error.
- The_rationalist 5y agoIt's such a shame that value classes in Valhalla can only be immutable... C# has mutable value based classes since years and this enable major optimizations in much more common use cases. This is also what everybody do in C. In addition to being less expressive and programmable, it should also be slower, e.g you have a value class that has ten fields, you just want to update one and ineptly, you have to reconstruct the whole object and reassign/copy every fields that have not changed.. Regarding ergonomics, they are totally broken, e.g you can't have an idiomatic Point. You cannot write point.x = 5
- ygra 5y agoMutable value types in C# have a place, but they also come with plenty of pitfalls. Java tends to lean in a direction where those don't exist. Cf. captured variables in closures as well: having to treat them as final avoids a number of problems.
- kaba0 5y agoWhile to my knowledge there is not yet a final decision, ‘withers’ are planned to solve that. https://github.com/openjdk/amber-docs/blob/master/eg-drafts/reconstruction-records-and-classes.md https://github.com/openjdk/amber-docs/blob/master/eg-drafts/... Basically, p = p with { .x = 3 } or something like that. But that will also planned to work for other kind of classes and will be a basis for advanced pattern matching as well. Also, in case of a primitive class with no guarantee of non-tearing, in practice you will get the same performance as with a mutable struct, maybe even better as the JVM can better reason about it.
- karmakaze 5y ago> Boxed ints have identity, whereas primitives do not; boxing is not able to fully paper over this gap. I don't know why the glossed over small values of Integer's sharing identity (effectively behaving like values) when discussing the complexity of these things. From 5.1.7. Boxing Conversion[0]: "If the value p being boxed is true, false, a byte, a char in the range \u0000 to \u007f, or an int or short number between -128 and 127, then let r1 and r2 be the results of any two boxing conversions of p. It is always the case that r1 == r2." [0] https://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.1.7 https://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html... I like this bit: > These so-called primitive classes can combine the expressive power of classes with the runtime behavior of primitives. The slogan for Valhalla is: Codes like a class, works like an int. I can immediately get a sense of what I'm getting--very much like Go using a non-pointer receiver for a function.
- kaba0 5y ago> I don't know why the glossed over small values of Integer's sharing identity It is mentioned in the second part if I remember correctly regarding changes to identity-dependent operations (namely ==).
- jollybean 5y agoI wonder if it's a better idea to drop reverse compatibility and simply have a Java2. Given how well the language is defined, they could almost certainly compile Jv1 classes to Jv2 classes anyhow with minimal impact probably. They could fix a couple other things along the way.
- MBCook 5y agoThe Java ecosystem is HUGE. It took python what, over a decade to get people to switch from 2 to 3 didn’t it? I’m just not sure you could ever pull it off. It seems way harder than that. Even with some sort of compatibility layer. I think you’d really end up in more of a perl 6 situation where it’s a different language that would eventually have to be renamed due to confusion.
- jollybean 5y agoThey may not have a choice, because J1 has problems evolving and it takes a decade (or never) to make major improvements. Just leave it and support it for another 20 years on the side. A lot of isolated Java could remain V1 no problem. Allow new starts, separate modules, and those that want to port a J2. Python 3 was a disaster because they never bothered to think about it, provided no facility for it, didn't care about collisions at the platform level. Oracle could literally pay for the most important OSS modules to be ported, give architectural guidance scripts to transform old codebases, special test units, training. Python and Javascript are ironically two of the biggest messed in all of tech, that gain popularity and drag everything into the mud with them.
- kinjba11 5y ago"Java 2" is already out - it's Kotlin (or Scala if you're so inclined) ;) Unfortunately the more compatible you are with "V1" the more compromises you have to make. See: Kotlin, F#, TypeScript bending over backwards to maintain compatibility with the dominant language of their respective platforms. Not to mention C++. You have to admire the hutzpah of Python 3 level of changes. Some software should simply have a "sell by" date and be thrown out when it starts to stink. That said I'm surprised more languages haven't attempted the approach of Rust with 'editions'. > There are times, however, when it is useful to be able to make small changes to the language that are not backwards compatible. The most obvious example is introducing a new keyword... Editions are the mechanism we use to solve this problem. When we want to release a feature that would otherwise be backwards incompatible, we do so as part of a new Rust edition. [https://doc.rust-lang.org/edition-guide/editions/index.html https://doc.rust-lang.org/edition-guide/editions/index.html]
- cletus 5y agoSo this touches on two ugly corners of Java: 1. No object value types; and 2. Boxing and primitives. On (1), all objects in Java are references, being a pointer to a pointer effectively. This indirection has a cost but has been incredibly useful historically because it enables moving allocation around the address space because none of the references change. They're effectively just pointers to a lookup table and it's the lookup table that changes. As for the example of packing an array of Points into a contiguous block of memory to avoid the dereferencing, I'll be curious to see what the proposal is here. Now, for example, if you have a collection of Points (x,y), you can put in a 3DPoint (x,y,z) that extends Point. You can't do that if you've flattened storage, not without some overhead at least. For years it's been the dream in Java to have true value Object types. You can do this in C++, which also allows you to allocate onto the stack. My position is that the complexity cost of this is massive. Copy constructors, move constructors, assignment operators, implicit constructors, implicit casts, etc. I mean you can have class A with instance a1 on the stack and a2 on the heap and pass &a1 and &a2 to a A* parameter and the callee has absolutely no idea of the lifetime of those or where they're allocated. But still, it would be nice to pass around an SHA1 hash as a 20 byte value instead of a reference to an object that contains a reference to an array that contains 20 byte elements. As for (2), this was a deliberate design choice in Java 5 (1.5) when generics were added. The decision was made, for better or for ill, to retain backwards compatibility through type erasure such that a List<T> is a List. This decreased the pain of upgrading and legacy code but created this ugly corner of primitive types. C# 2.0 came along later and decided to go the other way such an IList<T> is not an IList (IIRC the types; I'm not a C# guy). There are pros and cons. The fact that certain values are guaranteed to satisfy reference equality is kind of weird and (IMHO) confusing ie new Integer(127).equals(new Integer(127)) is true but new Integer(128).equals(new Integer(128)) isn't necessarily true. I guarantee you a lot of people don't know that.
- hashmash 5y ago> They're effectively just pointers to a lookup table and it's the lookup table that changes. What makes you think this is the case? Pointers to objects are direct, and garbage collectors freely change pointer values whenever it decides to relocate objects. Some collectors might use temporary forwarding pointers, to support concurrent collection.
- Const-me 5y agoStarted 7 years ago, delivered nothing? I don't think they gonna make it.
- kaba0 5y agoAs mentioned, it is “7 PHd’s knitted together” worth of change, especially in a don’t break existing code way. It is arguably the most ambitious language related project out there.
- esarbe 5y agoI guess if you want to maintain compatibility with all the existing ecosystem, it's better to be extra careful.
- pjmlp 5y agoThink C++ concepts and modules, or .NET AOT delivery timelines.
- fnord77 5y agoI have to say his writing style is very easy to follow. I tend to get distracted when reading technical text, but this was very easy to stay on track with
- x-shadowban 5y ago>there is no type that is the equivalent of List<?> in Java. std::vector<void*> ?
- vbezhenar 5y agovoid* is Object.
- pjmlp 5y agostd::vector<std::variant<T>> or std::vector<std::any<T>> then.
- adament 5y agoI do not know Java very well, but from the article I got the impression that in Java you could use List<?> as a reference in non-generic code to a List of arbitrary type. I.e. you could assign: List<?> variable = new ArrayList<Integer>(); That is not possible with std::vector<std::any>> in C++ without converting and allocating a new vector. I am not familiar enough with the C++ standard to say it definitively but similar rules apply for vectors of pointers see https://godbolt.org/z/no3r67P5z https://godbolt.org/z/no3r67P5z
- pjmlp 5y agoYou are missing the part that in Java generics (at least into Valhalla) are a type system feature. What the JVM ends up seeing is something like Java 1.4 bytecode would look like, and there is no need for allocations, because it is only references and generics don't support primitive types. While C++ templates are monomorphic so a few more tweeks are indeed needed all the way down to machine code. The principle of cleaning the underlying type applies though.
- deleted 5y ago[deleted]
- vbezhenar 5y ago
- esarbe 5y agoI'm really curious how this will influence the implementation of value classes and opaque types in Scala. I do hope that this will lead to a more efficient encoding, although I wonder how this would be possible without breaking backward compatibility.
- kaba0 5y agoI am not too knowledgable about Scala, but I think they only promise source-compatibility, so they are free to swap out the resulting binary to a more efficient implementation if the semantics are correct. Hopefully they did anticipate JVM-native value classes.
- boroboro4 5y agoI don't think it will affect opaque types implementation at all, since they exist only in compile time. As for value classes - Valhalla is a reason (Scala's) value classes weren't removed from Scala 3 (in favor of opaque types), since they seem to be a good candidate to be implemented using Valhalla's classes.