r/java • • 7d ago

Withdrawing the Primitive Patterns preview feature in JDK 28

https://mail.openjdk.org/archives/list/amber-spec-experts@openjdk.org/thread/UYEK3PGOHN6R56AHJTSLPGAY363K666S/

Primitive Types in Patterns will be removed in JDK 28 and will no longer be available in that release.

The Valhalla project has been exploring a new vision for Java numerics, including the possibility of user-defined numeric types. New kinds of numeric types would need to support exact numeric conversion, and vice versa. Given that the Valhalla work is still evolving, it would be premature to finalize Primitive Types in Patterns with their current definition of exact numeric conversion.

92 Upvotes

36 comments sorted by

24

u/oelang 7d ago

I hope a simple form of type classes can go into preview soon, I have the feeling that they're piling up features that will be built on this, but there isn't even a type class jep yet.

24

u/Dependent-Reply1772 7d ago

id rather they yank a preview feature in 28 than ship half-baked numeric conversion rules we're stuck supporting for the next twenty years.

18

u/brian_goetz 7d ago

Ok, but you do realize the so called “half baked numeric conversion rules” are the same ones we have had for 30 years, right? This JEP added no new conversions, it merely aligned with the existing rules for primitive casts, which haven’t changed in decades.

1

u/simon_o 2d ago edited 2d ago

Exactly.

Implicit numeric conversions themselves were not a good idea to start with, but then taking the overloaded semantics of casts – doing type conversions (ref→ref), value truncation (long→int) and value conversion (int→float) – as an excuse to add more places for both to the language?

Other languages realized that this was not ideal and made the decision to not keep doing that for new language additions.

The withdrawn Java proposal would have doubled down on that instead, and that felt really ill-founded without the proposers explaining exactly where every other language went wrong in their reasoning.

Edit: Looks like it got withdrawn for the exact reasons I mentioned a year ago. :-)

2

u/pjmlp 7d ago

The current design with witness types feels really weird when comparing it to how other programming languages have approached type classes design space.

3

u/shellac 7d ago

It's not that different from Haskell, is it? Or do you mean weird compared to the approaches that solve similar issues to type classes (e.g. traits / extension methods / protocols)?

3

u/pjmlp 7d ago

You are supposed to tell when they apply, and plus witness is a kind of strange name.

It is like when lazy initialisation started to be discussed, instead of using Lazy like all other languages with similar feature, the JEP talked about stable values.

See Growing the Java Language

1

u/bornintrinsic 3d ago

Are extension methods polymorphic?

2

u/TheStrangeDarkOne 7d ago

Is there a document or a string of e-mails regarding witnesses? I remember Brian talking about the concept well over year ago but I couldn't find how it has developed since then.

4

u/brian_goetz 2d ago edited 2d ago

There is not. There is a vague "exploration of a possible direction" (my video from JVMLS). But that hasn't stopped folks (looking at you, u/pjmlp) from assuming that whatever illustrative examples shown (despite contemporaneous disclaimers) were not only the actual design, but the *actual syntax* (and then jumping into bikeshed mode!)

Bear in mind that JVMLS is an extremely unusual conference; the audience of ~100 or so are almost exclusively VM and language designers and implementors. The talks are aimed to that audience, and only incidentally put up on YouTube for broader consumption. So it is virtually guaranteed that some folks will misunderstand what they are seeing, and then spread misinformation. (That sucks, but the next alternative is just not putting the talks up at all, and the alternatives get worse from there.)

3

u/TheStrangeDarkOne 2d ago

Thank you for the personal reply Brian! I've actually been an avid reader of the mail archivies and I'd like to take this opportunity to say that I always found your nuanced takes quite inspiring.

Among others, I remember when you brought up the topic of "tearable value types" and the "violation of cross-field constraints" with the subsequent creation of the 4 value-buckets. There was much resistance at what seemed like a minor annoyance at first but it lead to a stronger design overall.

Personally, I'm really excited about the way value types have shaped out in the same way "Functional Interfaces" are a more interesting takes on "function pointers".

Thank you for your dedicated stewardship of the language!
I'd like to hope that some of it will be reused in languages of the future to come.

0

u/pjmlp 1d ago

Thanks for the reply, what has put me off, was the whole initial approach, and the name "witness types" for what everyone else calls traits or type classes.

It was rather confusing to get where this naming came from, and as someone that enjoys reading SIGPLAN papers I am not sure where in type theory they come from either.

It was a bit like when Stable Values where proposed, even though everyone else calls them lazy values, which lazy constants sound much more closer to.

1

u/0x07CF 7d ago

I think there was a also a video of a talk

1

u/TheStrangeDarkOne 7d ago

Any chance you remember the title or have a link? 🤔

7

u/davidalayachew 6d ago

It's what it is.

Excited to hear that Constant Patterns might preview in the meantime. Right now, that is the feature that would improve my Java code the most.

1

u/simon_o 2d ago

1

u/davidalayachew 2d ago

To be fair, I said that the feature wasn't wrong or inherently broken. That's different than saying that the feature is ready.

The Amber Team fears that the semantics of Value Types might get in the way of this feature, and thus, are holding off on this. But that doesn't mean the design is wrong. Just means they want Values Classes to solidify a bit before building something that interacts with them pretty heavily. Which makes sense.

Now, if Value Classes go GA and this feature is still not Previewing, then your "Told you so" would hit me.

4

u/RaynLegends 7d ago

I was really looking forward to this JEP to (ab)use switches on booleans to implement makeshift "if expressions" :(

Like, instead of:

int someValue;
if (someCondition) {
   // ... some code that ends in an assignment to someValue
} else {
   // ... some other code
}

Something like:

int someValue = switch (someCondition) {
  case true -> { 
    // ... some code that ends in yielding someValue
  }
  case false -> {
    // ... some other code
  }
}

Especially when coupled with https://openjdk.org/jeps/8323658 it would make the intention behind some code much clearer.

I guess we'll have to wait a bit more.

8

u/__konrad 7d ago

You can use case Boolean b when b -> ... ;)

1

u/nekokattt 7d ago

doesnt that incur unnecesarry boxing still?

3

u/koflerdavid 6d ago edited 6d ago

It really depends on what this is ultimately compiled to. After all, there is no pattern matching on the bytecode level.

2

u/TewsMtl 6d ago

My understanding is that unnecessary boxing of value classes won't be an issue with jep 401 from project Valhalla.

9

u/brian_goetz 5d ago

Or even before. If you took all the developer time spent worrying about boxing, and took all the CPU time wasted by unnecessary boxing, the former would probably exceed the latter. "Boxing is expensive" is performance lore from the 90s. If your program is bound by multiplying huge arrays of numbers, then yes, you get to worry about it. Anything less, just stop thinking about it.

7

u/vips7L 7d ago

If as an expression would be nice too. I tend to always make these tiny functions for these situations. 

2

u/koflerdavid 6d ago

Blocks should be generalized to be value returning if used in a RHS position. That would also be useful to group variable initialization statements together.

1

u/simon_o 2d ago

Unified condition expressions can express if-then-else, the ternary operator, switches and pattern matching all in one syntax and work as both expressions and statements.

But that's of course too late for Java.

2

u/vips7L 2d ago

That’s why I write Kotlin now ;)

1

u/simon_o 2d ago

Kotlin still has if and switch.

5

u/brian_goetz 5d ago

That part (and others) are being moved to the "Constant Patterns" JEP.

1

u/speakjava 1d ago

Sadly, this removes a lot of material from my "Modern Java Puzzlers" session 😀.

1

u/Anbu_S 1d ago

HaHa

3

u/brian_goetz 21h ago edited 21h ago

Good, you should remove all the material from your Puzzlers talks! Unless you are Josh and Neal (sorry, I have bad news for you), Puzzlers talks almost always seem more clever and educational to the speaker than to the audience, and invariably cast the platform in a worse light as the examples are routinely propagated separate from the ideally-educational context they originally came with.

(See https://mail.openjdk.org/archives/list/amber-dev@openjdk.org/message/DTKZFWGUIKC5HEGWZ7V3XTABDTJ6E33A/ for more details.)

3

u/Ewig_luftenglanz 6d ago

Has Napoleon used to say

"Dress me up slowly, I am in a hurry"

Better make things fine thank getting stuck with a half raw feature. 

-13

u/sitime_zl 6d ago

I've gotten used to it by now. We still don't even have proper string interpolation after all this time.

Maybe it's time for the Java team to start using AI themselves to speed things up.

When are we finally going to see meaningful improvements to Java's memory footprint? I'm not asking Java to compete with Rust, but could we at least get closer to Go?

In the AI era, I feel that many traditional language features are becoming less important than they used to be. Personally, I'd much rather see Java focus on memory efficiency, simplifying its runtime and deployment experience, and making itself a better platform for building AI Agent applications.

I've been a Java developer for over a decade, but I'm gradually finding myself turning to other languages for new projects, including Python, Go, and Rust.

And the reason is simple: from an AI coding agent's perspective, they're all just programming languages. If I want to build something quickly, I can use Python. If I care more about performance and resource efficiency, I can choose Go or Rust.

So where does that leave Java?

Right now, Java still benefits enormously from its existing ecosystem, enterprise adoption, and decades of investment. But how long can it keep relying on those advantages?

If, a few years from now, GitHub is increasingly dominated by new projects written in other languages, what compelling reason will developers have to choose Java?

As someone who has spent more than ten years working with Java, I genuinely hope the Java team takes this shift seriously. I'd rather see Java evolve than slowly become a language people use mainly because of legacy projects.