https://kotlinlang.org logo
#language-evolution
Title
# language-evolution
s

stantronic

12/04/2019, 12:48 PM
Is there / could there be any way of enforcing named arguments in Kotlin? E.g. adding an annotation of “@UseNamedArgs” or similar would mean that the compiler would throw an error if that function was being called without using named arguments. Could help with code readability etc.
m

Mike

12/04/2019, 1:37 PM
I'm sure it's possible, but extending this out, why should a library developer be able to force my team to use named arguments if we don't want to? And if you want them for readability of your code, doesn't that come down to the team doing code reviews and enforcing readability standards that way/
k

karelpeeters

12/04/2019, 3:37 PM
Because the library author might not want to have to keep the order backwards compatible. The same thing is going on in Python, I think they've added forced named parameters now.
Or the other way around, but the same argument works there.
m

Mike

12/04/2019, 4:42 PM
Interesting. Could impact performance of compiler, as I think every function call would have to see if the function was annotated, but that info could likely be cached.
It would still feel 'weird' to me to be told I must use named parameters... If it's a Kotlin only library, and once
inline class
is non-experimental, we'd see less need of this (assuming the library could be more strongly typed, thereby eliminating confusion of types. But until then...
k

karelpeeters

12/04/2019, 4:45 PM
That doesn't really help, they might not want to guarantee the order of constructor parameters either.
m

Mike

12/04/2019, 4:47 PM
Wouldn't the constructor use inline classes for its parameters too? Regardless, answering OP, I'm not aware of any technical reason it can't be done, but I also don't have all the answers.
👍 1
q

Quy D X Nguyen

12/05/2019, 5:56 AM
I'm trying to see if it's possible to write a compiler plugin for this. I ran into some use cases where it would be useful.
s

stantronic

12/05/2019, 12:29 PM
Cool, let us know how you get on
4 Views