Add Either type with left, right and match functions

This commit is contained in:
Stypox 2025-01-28 11:12:06 +01:00
parent c2b698491b
commit e276d706d6
No known key found for this signature in database
GPG Key ID: 4BDF1B40A49FDD23

View File

@ -0,0 +1,25 @@
package org.schabi.newpipe.util
import androidx.compose.runtime.Stable
import kotlin.reflect.KClass
import kotlin.reflect.cast
import kotlin.reflect.safeCast
@Stable
data class Either<A : Any, B : Any>(
val value: Any,
val classA: KClass<A>,
val classB: KClass<B>,
) {
inline fun <R> match(ifLeft: (A) -> R, ifRight: (B) -> R): R {
return classA.safeCast(value)?.let { ifLeft(it) }
?: ifRight(classB.cast(value))
}
companion object {
inline fun <reified A : Any, reified B : Any> left(a: A): Either<A, B> =
Either(a, A::class, B::class)
inline fun <reified A : Any, reified B : Any> right(b: B): Either<A, B> =
Either(b, A::class, B::class)
}
}