chanjungskim
01/19/2023, 9:34 AMinterface BaseClass{
fun foo()
fun bar()
}
class ChildClass: BaseClass {
override fun foo()
override fun bar()
}
My expectation is,
class ChildClass: BaseClass {
override suspend fun foo()
override suspend fun bar()
}
Is that possible?hfhbd
01/19/2023, 9:43 AMinterface BaseClass{
suspend fun foo()
suspend fun bar()
}
chanjungskim
01/19/2023, 9:50 AMhfhbd
01/19/2023, 9:56 AMCLOVIS
01/19/2023, 9:59 AMsuspend
functions from regular functions. Imagine if you could do what you wanted:
fun main() { // non-suspend main
val a: BaseClass = ChildClass()
// we're calling the 'a' function of the base class, which is not suspend, that's OK
a.foo()
}
What would happen? It can't call the overriden function, because it's suspend
and we're in a function where suspend
is not allowed.Joffrey
01/19/2023, 2:44 PMsuspend
function technically has a different signature (in bytecode), so it's not a valid override for a non-suspend functionthen every child class will override suspend fun funtions@chanjungskim What is the problem with this?
CLOVIS
01/19/2023, 2:45 PMJoffrey
01/19/2023, 2:46 PMsuspend
function has a different contract from a regular functionCLOVIS
01/19/2023, 2:47 PMkrzysztof
01/19/2023, 3:47 PMchanjungskim
01/19/2023, 6:43 PMCasey Brooks
01/19/2023, 7:00 PMsuspend
function and then never suspend within it, any more than overriding a function and ignoring parameters passed to it that you don’t need. But it communicates that the code is intended to run async, may be running on background threads rather than the main thread, and helps with handling errors thrown within it. So even if you’re not strictly calling any suspend
functions within the Datadog implementation, there is still usefulness to having the method marked suspendCLOVIS
01/19/2023, 7:02 PMsuspend
functions are colored differently than regular functions, they can never be compatible. You want to create a common ancestor, but synchronous and asynchronous are very different things.uli
01/20/2023, 2:41 PMJoffrey
01/20/2023, 2:43 PMsuspend
methods. Technically they are aware of the suspend
keyword and should implement the function accordingly (at least with some withContext()
to dispatch to another thread)