r/java • • Jan 22 '26

Carrier Classes; Beyond Records - Inside Java Newscast

https://youtu.be/cpGceyn7DBE
88 Upvotes

81 comments sorted by

View all comments

Show parent comment

5

u/javaprof Jan 22 '26

Yes, basically named arguments can be de-sugared into regular call by the compiler.
Supporting default arguments would be tricky tho

3

u/melkorwasframed Jan 22 '26

Parameter names are still not preserved in the bytecode by default though right?

1

u/vqrs Jan 22 '26

They don't have to be if it happens at compile time.

2

u/manifoldjava Jan 23 '26

They do if the method you're calling is compiled. But this wouldn't necessarily require bytecode revision e.g., an annotation could preserve the names.

1

u/javaprof Jan 23 '26

1

u/manifoldjava Jan 23 '26

Right. But naming arguments is necessary only for methods with one or more default parameters. If 118 could be altered to silently target those methods, sure.

2

u/vips7L Jan 22 '26

How are defaults handled with annotations? Seems like there is already some support there for both these features. 

2

u/Eav___ Jan 23 '26 edited Jan 23 '26

It's a different story FYI. Annotation arguments are backed by methods, which support defaults and are exported from the very beginning, but normal parameters lack a lot of these infrastructure.

But I suppose introducing named parameters for carrier classes / records could be a good starting point as they already have some of these.

4

u/segv Jan 22 '26 edited Jan 22 '26

If you could recompile the world then sure, but since this would have to work without recompilation[1] - I'm not so sure anymore.

Let's say we have an application that has a Dependency A. Dependency A calls a method in Dependency B using named parameters. With this de-sugaring by the compiler (or compiler callsite magic, as it was called before), the DepA would work fine as long as DepB was exactly in the version DepA was compiled against.

Now, since the programmer used named parameters, what would happen if a newer version of DepB swapped argument order around? The programmer referred to an argument by name, so it would be a reasonable expectation that the platform would somehow do the right thing here.

Without support from the JVM, if this feature was just compiler[1] smoke & mirrors then either:

  • a MethodNotFoundException would be thrown if signature changed, or
  • ParamA would suddenly have a value meant for ParamB if the types accidentally matched and the signature did not change (think SomeType method(int paramA, int paramB)), or
  • the compiler would have to inject a hefty amount of bytecode at the callsite to get the parameters via reflection and then figure out the right invocation, costing a bunch of CPU cycles.

Frankly, it would be a disaster. On the other hand the support from JVM would probably be pretty expensive to implement, especially with projects like Parametric JVM on the horizon. I kinda see why the team at Oracle is prioritizing other features.

[1] By recompilation I assume javac, not C1/C2.

3

u/vowelqueue Jan 22 '26

I'm pretty sure that Kotlin just throws a NoSuchMethod and expects library developers to use traditional overloading if they don't want to break binary compatibility.

2

u/vqrs Jan 22 '26

Yes, it would be a reasonable assumption. But many things are reasonable assumptions until you know better.

Since it's susceptible to the exact same issue with parameter reordering as is not using parameter but gives you benefits while writing code, I don't really think this is a big deal.

2

u/vips7L Jan 22 '26

Why can we have default values and named parameters in annotations then? Are we not worried about the same things there? Does the vm explicitly support them?

2

u/vowelqueue Jan 23 '26

Yeah, the JVM supports encoding full name-value pairs for annotation arguments.

1

u/javaprof Jan 23 '26

> Now, since the programmer used named parameters, what would happen if a newer version of DepB swapped argument order around? The programmer referred to an argument by name, so it would be a reasonable expectation that the platform would somehow do the right thing here.

Usually, named arguments are optional, so it can't be reasonable expectation. There are not giving runtime safety, just like Generics. Your assumption might be correct in some structured typed world (javascript) where name becomes key in some dynamic structures.