From 95767bc9925f328c1b73a0672d82afb39ae95a6b Mon Sep 17 00:00:00 2001 From: Simon Vergauwen Date: Tue, 20 Sep 2022 18:28:55 +0200 Subject: [PATCH] Make isRight smart contracted --- .../commonMain/kotlin/arrow/core/Either.kt | 21 ++++++++++--------- .../kotlin/arrow/core/EitherTest.kt | 14 ++++++++----- 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/Either.kt b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/Either.kt index 345877d8618..65a8f074a3c 100644 --- a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/Either.kt +++ b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/Either.kt @@ -787,17 +787,18 @@ public sealed class Either { @JsName("_isLeft") internal abstract val isLeft: Boolean - @Deprecated( - RedundantAPI + "Use `is Either.Left<*>`, `when`, or `fold` instead", - ReplaceWith("(this is Either.Left<*>)") - ) - public fun isLeft(): Boolean = isLeft + @OptIn(ExperimentalContracts::class) + public fun isLeft(): Boolean { + contract { returns(true) implies (this@Either is Left) } + return this@Either is Left + } - @Deprecated( - RedundantAPI + "Use `is Either.Right<*>`, `when`, or `fold` instead", - ReplaceWith("(this is Either.Right<*>)") - ) - public fun isRight(): Boolean = isRight + + @OptIn(ExperimentalContracts::class) + public fun isRight(): Boolean { + contract { returns(true) implies (this@Either is Right) } + return this@Either is Right + } /** * Transform an [Either] into a value of [C]. diff --git a/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/EitherTest.kt b/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/EitherTest.kt index 3a847c12abf..1a50893d197 100644 --- a/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/EitherTest.kt +++ b/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/EitherTest.kt @@ -12,6 +12,7 @@ import arrow.core.test.generators.suspendFunThatReturnsEitherAnyOrAnyOrThrows import arrow.core.test.generators.suspendFunThatThrows import arrow.core.test.laws.MonoidLaws import arrow.typeclasses.Monoid +import io.kotest.assertions.fail import io.kotest.assertions.throwables.shouldThrow import io.kotest.matchers.shouldBe import io.kotest.property.Arb @@ -48,15 +49,18 @@ class EitherTest : UnitSpec() { "isLeft should return true if Left and false if Right" { checkAll(Arb.int()) { a: Int -> - Left(a).isLeft() && !Right(a).isLeft() - (Left(a) is Left<*>) && !(a.right() is Left<*>) - } + val x = Left(a) + if(x.isLeft()) x.value shouldBe a + else fail("Left(a).isLeft() cannot be false") + x.isRight() shouldBe false } "isRight should return false if Left and true if Right" { checkAll(Arb.int()) { a: Int -> - !Left(a).isRight() && Right(a).isRight() - (a.left() !is Right<*>) && (Right(a) is Right<*>) + val x = Right(a) + if(x.isRight()) x.value shouldBe a + else fail("Right(a).isRight() cannot be false") + x.isRight() shouldBe false } }