https://kotlinlang.org logo
Title
n

Nicolas Mariano Obregon

11/24/2020, 3:33 PM
<<SOLVED>> Hi everyone! I’m new to Kotlin and functional programming, coming from Java OO. Here’s my question: If I want to define some interface method implementations using
runCatching
, something like this below, how should I define my interface method signature in order to return whatever the runCatching returns? I hope I was clear enough. Thanks in advanced
interface IMyInterface { 
    suspend fun myFunction()
}

class MyClass : IMyInterface {
    override suspend fun myFunction() = runCatching {

    }.onFailure { ... }.getOrThrow()
}
v

Vampire

11/24/2020, 3:35 PM
Would you mind using three backticks for a block instead of one per line, then it is much more readable
n

Nicolas Mariano Obregon

11/24/2020, 3:45 PM
yes! my bad
interface IMyInterface { 
    suspend fun myFunction()
}

class MyClass : IMyInterface {
    override suspend fun myFunction() = runCatching {
        ...
    }.onFailure { ... }.getOrThrow()
}
v

Vampire

11/24/2020, 3:49 PM
And you can edit messages in Slack. 😄
Regarding the actual question, well whatever you return inside the
runCatching
is the return type.
Or did I get your question wrong?
s

StavFX

11/24/2020, 4:03 PM
Your interface shouldn't care about whether or not the implementation will use runCatching. Just define the return type you normally would
n

Nicolas Mariano Obregon

11/24/2020, 4:55 PM
thanks everyone, it was a PIBKAC issue, I didn’t write this part
}.onFailure { ... }.getOrThrow()
And the compiler was complaining, but after adding those, it worked fine. Thanks!
👌 2