Convert some sealed classes to interfaces (#4347)

There is no non-abstract field in them, we can just fall back to
interfaces.
This commit is contained in:
Zongle Wang 2024-03-30 03:11:53 +08:00 committed by GitHub
commit 1acae50845
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 43 additions and 40 deletions

View file

@ -21,27 +21,29 @@ package com.keylesspalace.tusky.util
* Class to represent sum type/tagged union/variant/ADT e.t.c.
* It is either Left or Right.
*/
sealed class Either<out L, out R> {
data class Left<out L, out R>(val value: L) : Either<L, R>()
data class Right<out L, out R>(val value: R) : Either<L, R>()
sealed interface Either<out L, out R> {
data class Left<out L, out R>(val value: L) : Either<L, R>
data class Right<out L, out R>(val value: R) : Either<L, R>
fun isRight() = this is Right
fun isRight(): Boolean = this is Right
fun isLeft() = this is Left
fun isLeft(): Boolean = this is Left
fun asLeftOrNull() = (this as? Left<L, R>)?.value
fun asLeftOrNull(): L? = (this as? Left<L, R>)?.value
fun asRightOrNull() = (this as? Right<L, R>)?.value
fun asRightOrNull(): R? = (this as? Right<L, R>)?.value
fun asLeft(): L = (this as Left<L, R>).value
fun asRight(): R = (this as Right<L, R>).value
inline fun <N> map(crossinline mapper: (R) -> N): Either<L, N> {
return if (this.isLeft()) {
Left(this.asLeft())
} else {
Right(mapper(this.asRight()))
companion object {
inline fun <L, R, N> Either<L, R>.map(crossinline mapper: (R) -> N): Either<L, N> {
return if (this.isLeft()) {
Left(this.asLeft())
} else {
Right(mapper(this.asRight()))
}
}
}
}