Is there / could there be any way of enforcing nam...
# language-evolution
s
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
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
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
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
That doesn't really help, they might not want to guarantee the order of constructor parameters either.
m
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
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
Cool, let us know how you get on