<<SOLVED>> Hi everyone! I’m new to Ko...
# getting-started
n
<<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
Copy code
interface IMyInterface { 
    suspend fun myFunction()
}

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

    }.onFailure { ... }.getOrThrow()
}
v
Would you mind using three backticks for a block instead of one per line, then it is much more readable
n
yes! my bad
Copy code
interface IMyInterface { 
    suspend fun myFunction()
}

class MyClass : IMyInterface {
    override suspend fun myFunction() = runCatching {
        ...
    }.onFailure { ... }.getOrThrow()
}
v
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
Your interface shouldn't care about whether or not the implementation will use runCatching. Just define the return type you normally would
n
thanks everyone, it was a PIBKAC issue, I didn’t write this part
Copy code
}.onFailure { ... }.getOrThrow()
And the compiler was complaining, but after adding those, it worked fine. Thanks!
👌 2