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.
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.
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.
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.
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
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.
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.
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.
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.
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.
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.
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?
> 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.
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.)
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
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.
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.