Named arguments are really useful and makes code m...
# language-proposals
j
Named arguments are really useful and makes code much easier to read, especially when you have to call functions that have many parameters. Unfortunately, named arguments aren't allowed when doing calls to Java for example. Since Java is also statically typed, is it not possible to make named arguments work with Java functions as well? Or would this be an issue when interopting with JavaScript? Proposal: allow calling Java functions from Kotlin using Named Arguments
g
By default Java bytecode doesn’t include argument names, so for most of existing published libraries it wouldn’t work anyway
there is javac option to keep argument names, but main concern which I know about this is that in Java argument name is not considered as backward incompatible change, because there is no call by named argument, so it would break existing Kotlin code that uses named arguments for Java
j
The idea is that when you compile and it's interopting with Java, that it simply inlines those arguments in the right order and any missing arguments, fill with a null.
g
yes, but than you update the library and you have no source incompatible change
I don’t think that missing argument should be populated with null, this is not how it works even in Kotlin
j
Java:
Copy code
String doSomething(Integer param1, String param2)
Kotlin:
Copy code
doSomething(12, "test")
or
Copy code
doSomething(param1 = 12, param2 = "test")
This way, existing Kotlin code interopting with Java still works You could make all the named parameters required when interopting with Java, that way, there are no null pointer surprises, unless you explicityly send in a null
Copy code
doSomething(param1 = null, param2 = "test")
d
And then the library changes their parameter names and suddenly your code no longer compiles.
g
And such change wouldn’t be even considered by Java library as backward incompatible
And there is still a problem with the fact, that most of published Java libraries doesn’t have argument names in bytecode
s
Or would this be an issue when interopting with JavaScript?
I don’t think this would be an issue. JavaScript and Java interop are separate things. By the way, calls to JavaScript functions already allow named arguments because Kotlin/JS requires explicit Kotlin declarations for external functions.
👍 1