Hi everyone, I'm writing 2 classes to represent lo...
# announcements
i
Hi everyone, I'm writing 2 classes to represent long running operation state. This works but I wonder if there's a way to improve it (e.g. by making both share an interface)?
Copy code
sealed class VoidOperationState {
    object Progress: VoidOperationState()
    object Success: VoidOperationState()
    data class Failure(val t: Throwable): VoidOperationState()
}

sealed class OperationState<out T> {
    object Progress: OperationState<Nothing>()
    data class Success<out T>(val data: T): OperationState<T>()
    data class Failure(val t: Throwable): OperationState<Nothing>()
}
(Of course I could make
VoidOperationState
just an
OperationState<Unit>
but that's not nice, since I'd have to be always passing the
Unit
to
Success
...
z
It’s a tradeoff, but I would probably consider having a single type hierarchy worth the teeny bit of boilerplate of passing in
Unit
.
You can also create a factory function named
Success
that takes no arguments and returns
Success(Unit)
to get rid of the boilerplate.
i
You're right, using
Unit
isn't so bad
also can create a type alias to alleviate a part of it