commonMain.com.copperleaf.json.utils.Either.kt Maven / Gradle / Ivy
package com.copperleaf.json.utils
public sealed class Either {
public data class Left(val value: LeftT) : Either()
public data class Right(val value: RightT) : Either()
}
// Creation functions
// ---------------------------------------------------------------------------------------------------------------------
public fun L.asLeft(): Either {
return Either.Left(this)
}
public fun R.asRight(): Either {
return Either.Right(this)
}
public inline fun Any.asEither(): Either {
return if (this is L) {
this.asLeft()
} else if (this is R) {
this.asRight()
} else {
error("$this is neither ${L::class.simpleName} nor ${R::class.simpleName}")
}
}
// Access values
// ---------------------------------------------------------------------------------------------------------------------
public fun Either.requireLeftValue(): L {
return (this as Either.Left).value
}
public val Either.leftValue: L?
get() {
return (this as? Either.Left)?.value
}
public fun Either.requireRightValue(): R {
return (this as Either.Right).value
}
public val Either.rightValue: R?
get() {
return (this as? Either.Right)?.value
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy