Skip to content

Commit

Permalink
Make isRight smart contracted
Browse files Browse the repository at this point in the history
  • Loading branch information
nomisRev committed Sep 20, 2022
1 parent 59feb7c commit 95767bc
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -787,17 +787,18 @@ public sealed class Either<out A, out B> {
@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<A>) }

This comment has been minimized.

Copy link
@raulraja

raulraja Sep 20, 2022

Member

nice!

return this@Either is Left<A>
}

@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<B>) }
return this@Either is Right<B>
}

/**
* Transform an [Either] into a value of [C].
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
}

Expand Down

0 comments on commit 95767bc

Please sign in to comment.