https://kotlinlang.org logo
Title
g

GarouDan

10/02/2019, 5:37 PM
Hi, if we have a method on a abstract class, like this:
abstract fun test() : String
can we, using somehow the new kotlin techniques to change the returning value of the function in the other abstract class which extend the first one, something like this?
fun test(): String {
    return test().toString()
}

fun test(): Int {
}
The goal would be use to reuse the same name. Maybe something using the
by
keyword somehow? Composing the objects (where the problem here is that I have two abstract classes)
s

Shawn

10/02/2019, 5:38 PM
No, you’d be breaking the contract specified by the class you’re inheriting from
and I’m pretty sure there isn’t any sane way to overload a no-arg method
if your real
test
method can take different parameters than specified in the parent class then sure, there’s nothing stopping you. Otherwise, no, what you’ve described is not possible
d

Diego Almeida de Oliveira

10/02/2019, 5:44 PM
You could use
generics
k

karelpeeters

10/02/2019, 6:14 PM
Generics don't help either I think?
👆 1
d

Diego Almeida de Oliveira

10/02/2019, 6:59 PM
You are right. The thing is, it's not possible to have two functions in the same class with same signature. And the return type doesn't define the signature, meaning that both
test
functions have same signature,
g

GarouDan

10/02/2019, 8:58 PM
It looks like we can if the second type inherits from the first one, like this:
a

Alowaniak

10/02/2019, 9:13 PM
Yea that's covariant return type It's legal because a subtype (should) honors the same contract as it's super type
💯 2
s

Stephan Schroeder

10/03/2019, 10:09 AM
That (via the
covariant
keyword) is actually something that Dart has but Kotlin doesn’t https://dart.dev/guides/language/sound-problems#the-covariant-keyword