https://kotlinlang.org logo
#codingconventions
Title
# codingconventions
g

Garret Yoder

03/06/2024, 8:09 PM
Is there a reason that neither an abstract function or it's eventual override implementation can supply default argument values?
s

Szymon Jeziorski

03/07/2024, 7:58 AM
Only second part of the statement is true. Super (parent) functions (open functions, abstract functions or functions of an interface) are perfectly allowed to have default arguments: https://pl.kotl.in/uIEVopqzZ Their implementations however do not. I was thinking about this myself one day and found this answer on Stack Overflow, which seems reasonable to me https://stackoverflow.com/a/37701188
k

Klitos Kyriacou

03/07/2024, 9:37 AM
I think this can be a bit confusing, if you see
foo.bar()
being called when the method is declared to take a parameter, but you don't see that there's a default because the default is in a superclass (possibly a great-grandparent class).
👍 1
g

Garret Yoder

03/07/2024, 2:08 PM
Sure, but I think we should be able to supply default values in abstract methods.
the default value is irrelevant to it's eventual implementation
It'd also be fairly easy to avoid the confusion of "I didn't realize this had a default from it's parent declaration" by requiring a default value to be declared if the declaration had a default value, same as we do with any given argument. you could even just use super or some other keyword to say use the parent default, though this might be slightly confusing class inheritance vs functions. eg;
override fun foo(value: Int = super)
Right now because of this it's not possible to do optional arguments in an overridden function