Skip to content

Commit

Permalink
Moved things around
Browse files Browse the repository at this point in the history
  • Loading branch information
rcardin committed Apr 12, 2024
1 parent c5aebe0 commit e4680aa
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 22 deletions.
6 changes: 6 additions & 0 deletions src/main/scala/in/rcard/raise4s/Builders.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ package in.rcard.raise4s
def either[A, Error](block: Raise[Error] ?=> A): Either[Error, A] =
fold(block, error => Left(error), value => Right(value))

object EitherPredef:
extension [Error, A](either: Either[Error, A])(using r: Raise[Error])
def bind(): A = either match
case Right(a) => a
case Left(e) => r.raise(e)

class OptionRaise(val raise: Raise[Option[Nothing]]) extends Raise[Option[Nothing]]:
override def raise(error: Option[Nothing]): Nothing = raise.raise(error)

Expand Down
6 changes: 0 additions & 6 deletions src/main/scala/in/rcard/raise4s/Raise.scala
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,3 @@ def withError[Error, OtherError, A](
block: Raise[OtherError] ?=> A
)(using r: Raise[Error]): A =
recover(block, otherError => r.raise(transform(otherError)))

object EitherPredef:
extension [Error, A](either: Either[Error, A])(using r: Raise[Error])
def bind(): A = either match
case Right(a) => a
case Left(e) => r.raise(e)
21 changes: 20 additions & 1 deletion src/test/scala/in/rcard/raise4s/BuildersSpec.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package in.rcard.raise4s

import in.rcard.raise4s.EitherPredef.bind
import in.rcard.raise4s.OptionPredef.bind
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers
Expand All @@ -14,6 +15,24 @@ class BuildersSpec extends AnyFlatSpec with Matchers {
either { "success" } should be(Right("success"))
}

"Either.bind" should "return the value if it is not an error and raise an Error otherwise" in {
val one: Either[Nothing, Int] = Right(1)
val left: Either[String, Int] = Left("error")

val actual = either {
val x = one.bind()
val y = recover(
{
left.bind()
},
{ _ => 1 }
)
x + y
}

actual should be(Right(2))
}

"The option builder" should "create a None instance" in {
option { raise(None) } should be(None)
}
Expand All @@ -28,7 +47,7 @@ class BuildersSpec extends AnyFlatSpec with Matchers {

val actual = option {
val x = some.bind()
val y = recover({ none.bind() }, { _ => 1 })
val y = recover({ none.bind() }, { _ => 1 })
x + y
}

Expand Down
15 changes: 0 additions & 15 deletions src/test/scala/in/rcard/raise4s/RaiseSpec.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package in.rcard.raise4s

import in.rcard.raise4s.EitherPredef.bind
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers

Expand Down Expand Up @@ -141,18 +140,4 @@ class RaiseSpec extends AnyFlatSpec with Matchers {

actual should be(Left(5))
}

"Either.bind" should "return the value if it is not an error and raise an Error otherwise" in {
val one: Either[Nothing, Int] = Right(1)
val left: Either[String, Int] = Left("error")

val actual = either {
val x = one.bind()
val y = recover({ left.bind() }, { _ => 1 })
x + y
}

actual should be(Right(2))
}

}

0 comments on commit e4680aa

Please sign in to comment.