r/java • • Jan 22 '26

Carrier Classes; Beyond Records - Inside Java Newscast

https://youtu.be/cpGceyn7DBE
85 Upvotes

81 comments sorted by

View all comments

18

u/javaprof Jan 22 '26

So still no named arguments or any replacement for the same idea?
Why named arguments? Because builder and null restricted types doesn't work well together. Constructor allows to keep nullability information and at compile time proof that every parameter present.

15

u/Puzzleheaded-Eye6596 Jan 22 '26

records can get horrible to instantiate. named arguments would be so helpful

1

u/OwnBreakfast1114 Jan 23 '26

Honestly, we have a ton of like 15 parameter records and all arg constructors. I wouldn't say it's that bad, at least we get compiler errors everywhere when we add a new field to the record (most common update to a record by far).

Mutating state with a with doesn't get called out when you add a new field, which is convenient, but can hide bugs if you imagine that any with/copy constructor is actually an operation that someone should verify isn't broken with a new field addition.

→ More replies (2)

7

u/manifoldjava Jan 22 '26 edited Jan 22 '26

110% agree. The value of named/optional args compounds daily with proposed features.

edit: For those concerned about binary compatibility with named/optional args, the experimental manifold-params compiler plugin demonstrates otherwise - it is binary compatible while stretching beyond Kotlin's capabilities. Although the feature does complicate overload logic, that could be mitigated e.g., by prohibiting user-defined overloads on a method with defaults. Shrug.

3

u/Ok-Bid7102 Jan 22 '26 edited Jan 22 '26

Probably it would be fine even if we only had nominal features for records (and potentially these new carrier classes in the future).

Many other languages don't have named parameters, but they provide nominal construction for structures / objects. Example Javascript, Go, etc.
Does not seem to hinder the productivity of these languages.

Personally i would say the number of parameters on a method should be reasonably low, ideally less than 5, if a method needs more than that you can define a record with the properties you want, and take that as an argument.

2

u/Lucario2405 Jan 23 '26

Projects Valhalla & Lilliput should make that last point even more of a no-brainer in the future.

1

u/Puzzleheaded-Eye6596 Jan 23 '26

Even 3 parameters on record could become cumbersome (especially of the same type). I use records because i love immutability but deep deep deep down I just want to make a regular class with \@Builder lombok annotations

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.

→ More replies (2)

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.

→ More replies (3)

2

u/aoeudhtns Jan 22 '26 edited Jan 22 '26

In the short term, I think withers are going to help a lot. Right now the strawman syntax I've seen is usually like instance with { component = value }, but you could 1) use some static DEFAULTS instance, or you could just do that with a new one: new MyClass() with { component = value, ... } provided that you supply all the defaults in the default constructor. And you don't mind creating 2 instances to throw one away, in the latter imagining.

That doesn't help with method calls and other places where named arguments would help, but it could eliminate boilerplate Builder pattern in many places. (ETA: I am hoping that carrier classes and records alike both can participate in withers.)

3

u/javaprof Jan 23 '26

Using defaults with withers introducing possibility to use defaults in code.

Simple example:

We have some class Person and two developers working in own branches.

Developer 1: Adding new field, adding it to Person.DEFAULT, updating all cases where new Person derived from Person.DEFAULT
Developer 2: Using Person.DEFAULT to create derived instance.

Both MRs approved, tested and merged. In codebase we would have case when actual value not provided and Person using some default value.

So while this is clever workaround, it's far from desired solution. Basically it's a Builder with defaults set. Still bad design

2

u/aoeudhtns Jan 23 '26

I'm with you, I would also like named arguments. The one other difference here is that you don't need a dedicated builder with a build() method - the with { ... } effectively is the .build(). As long as your needs are simple enough to fit that mold, it eliminates the whole extra builder class.

2

u/javaprof Jan 23 '26

Yep, not having builder classes manual or lombok is great