Hi folks, I'm trying to create a data type aking to
Result<T>
but which 1) Can return an arbitrary type as error and 2) Doesn't have a success payload. Here's the solution I came up with. I wonder if it can be expressed more elegantly?
Copy code
sealed class EmptyResult<E> {
class Success<E> : EmptyResult<E>()
data class Failure<E>(val error: E): EmptyResult<E>()
}
fun foo(): EmptyResult<String> = EmptyResult.Success()
d
diesieben07
01/17/2020, 11:02 AM
I would make
Success
a singleton:
Copy code
sealed class EmptyResult<out E> {
object Success : EmptyResult<Nothing>()
data class Failure<out E>(val error: E): EmptyResult<E>()
}
fun foo(): EmptyResult<String> = EmptyResult.Success