r/java • • Jan 22 '26

Carrier Classes; Beyond Records - Inside Java Newscast

https://youtu.be/cpGceyn7DBE
86 Upvotes

81 comments sorted by

View all comments

Show parent comment

4

u/TomKavees Jan 22 '26 edited Jan 22 '26

My pet theory is that this functionality would require a ton of support from the JVM, including a fair bit of new bytecode ops, which would take a ton of work to do for relatively little actual benefit - there are other enhancements that give more bang for the buck.

Remember that this feature would not only have to handle $userCode->$userCode, but also $userCode->$randomJarDependency and $dependencyA->$dependencyB without recompilation, including version upgrades and name changes, i.e. compiler callsite magic to turn named parameters in user code into regular, explicit calls with all parameters at bytecode level is nowhere near enough.

10

u/vips7L Jan 22 '26

Why would it need bytecode or vm support? Both Kotlin and scala have these features running on the vm. 

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.

1

u/vytah Jan 23 '26

If you have two separately compiled modules, one has:

public void foo(int x, int y){

and second has (strawman syntax):

foo(x:1, y:2);

and then you change the first one to

public void foo(int y, int x){

you suddenly call foo with x=2 and y=1, which is explicitly not what you wanted.

Also, Java compiler by default omits argument names, so you'd have to change the defaults.

4

u/Absolute_Enema Jan 23 '26

How is this different from what already happens with positional parameters?

1

u/vytah Jan 24 '26

With positional parameters, you expect their position to be meaningful.

With named parameters, you expect their names to be meaningful.

JVM is exclusively based on positional parameters, so if you rearrange parameters, you are changing the semantics for all the callers. There's no bytecode instruction to call a method with named parameters. You can express "call method foo with first argument equal to 1 and second argument equal to 2", you cannot express "call method foo with argument x equal to 1 and argument y equal to 2".