Skip to content

Commit

Permalink
Renaming .? to .ok
Browse files Browse the repository at this point in the history
Also:
- old `.ok` was removed, use `.toOption`
- `.err` -> `.errOption`
  • Loading branch information
natsukagami committed Apr 18, 2024
1 parent 9fed188 commit 83bc101
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 32 deletions.
35 changes: 15 additions & 20 deletions src/main/scala/dotty/result/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import scala.annotation.implicitNotFound
* extension[T] (it: IterableOnce[T])
* def tryMap[U, E](f: T => Result[U, E]): Result[Seq[U], E] =
* Result:
* it.iterator.map(f(_).?) // shorts-circuit on the first Err and returns
* it.iterator.map(f(_).ok) // shorts-circuit on the first Err and returns
* }}}
*
* Tail-recursive functions can be implemented by using
Expand All @@ -36,7 +36,7 @@ import scala.annotation.implicitNotFound
* extension[T] (it: IterableOnce[T])
* def tryFoldLeft[U, E](init: U)(f: (U, T) => Result[U, E]) =
* Result:
* it.iterator.foldLeft(init)(f(_, _).?)
* it.iterator.foldLeft(init)(f(_, _).ok)
* }}}
*
* Conversions from [[Option]] and [[Either]] are available in
Expand Down Expand Up @@ -191,15 +191,10 @@ object Result:
case Ok(value) => Some(value)
case Err(_) => None

/** Returns the [[Ok]] value from the result. An alias of [[toOption]].
* @group convert
*/
inline def ok: Option[T] = toOption

/** Returns the [[Err]] error from the result.
* @group convert
*/
def err: Option[E] = r match
def errOption: Option[E] = r match
case Ok(_) => None
case Err(error) => Some(error)

Expand Down Expand Up @@ -423,7 +418,7 @@ object Result:
/** Evaluates `body`, returning the output as an [[Ok]] value.
*
* Within `body`:
* - [[eval.`?`]] can be used to unwrap [[Result]] values, with the body
* - [[eval.ok]] can be used to unwrap [[Result]] values, with the body
* short-circuiting back with the error.
* - [[eval.raise]] can be used to quickly short-circuit with an error.
* @group eval
Expand All @@ -435,7 +430,7 @@ object Result:
/** Operations that are valid under a [[Result.apply]] scope.
* @group eval
* @see
* [[eval.?]], [[eval.raise]] and [[eval.break]]
* [[eval.ok]], [[eval.raise]] and [[eval.break]]
*/
object eval:
/** Similar to [[Result.apply]]. */
Expand Down Expand Up @@ -479,8 +474,8 @@ object Result:
* seq match
* case Seq() => init
* case Seq(h, t*) =>
* // t.tryFoldLeft(f(init, h).?)(f).? // error: not a tail call (.? applied at the end)
* eval.break(t.tryFoldLeft(f(init, h).?)(f)) // ok
* // t.tryFoldLeft(f(init, h).ok)(f).ok // error: not a tail call (.ok applied at the end)
* eval.break(t.tryFoldLeft(f(init, h).ok)(f)) // ok
* }}}
*
* Note that however, in most cases, it is simpler to capture the
Expand All @@ -495,7 +490,7 @@ object Result:
* def loop(current: U, seq: Seq[T]): U = // note the return type
* seq match
* case Seq() => current
* case Seq(h, t*) => loop(f(current, h).?, t)
* case Seq(h, t*) => loop(f(current, h).ok, t)
* loop(init, seq)
* }}}
*
Expand All @@ -504,7 +499,7 @@ object Result:
* extension[T] (it: IterableOnce[T])
* def tryFoldLeft[U, E](init: U)(f: (U, T) => Result[U, E]): Result[U, E] =
* Result:
* it.iterator.foldLeft(init)(f(_, _).?)
* it.iterator.foldLeft(init)(f(_, _).ok)
* }}}
*
* @return
Expand Down Expand Up @@ -546,7 +541,7 @@ ${(resultsIncompatible ++ errorsIncompatible).mkString("\n")}
Perhaps you want to:
- Unwrap the `Result`, returning `${Type.show[T]}`:

${r.show}.?
${r.show}.ok

- Map the resulting value to another type with `.map` or `.mapError`

Expand All @@ -564,12 +559,12 @@ Perhaps you want to:

extension [T, E, E1](r: Result[T, E])(using
@implicitNotFound(
"`.?` cannot be used outside of the `Result.apply` scope."
"`.ok` cannot be used outside of the `Result.apply` scope."
)
label: boundary.Label[Err[E1]]
)(using
@implicitNotFound(
"""`.?` cannot be used here, as the error types of this Result (${E}) and `Result.apply` (${E1}) are incompatible. Consider changing the error type of `Result.apply`, or provide a conversion between the error types through an instance of the `Conversion` trait:
"""`.ok` cannot be used here, as the error types of this Result (${E}) and `Result.apply` (${E1}) are incompatible. Consider changing the error type of `Result.apply`, or provide a conversion between the error types through an instance of the `Conversion` trait:
given Conversion[${E}, ${E1}] = ???
Expand All @@ -585,15 +580,15 @@ Perhaps you want to:
* val err = Err("fail!")
*
* val compute = Result:
* ok.? // ok, unwraps and gives 1
* + err.? // error, immediately sets compute to Err("fail")
* ok.ok // ok, unwraps and gives 1
* + err.ok // error, immediately sets compute to Err("fail")
* + f() // not evaluated
* }}}
* @group eval
* @see
* [[apply]] and [[raise]].
*/
inline def `?` : T =
inline def ok: T =
r match
case Ok(value) => value
case Err(error) => raise(error)
Expand Down
6 changes: 3 additions & 3 deletions src/test/scala/result/Collections.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class CollectionTest extends munit.FunSuite {
extension [T](s: Iterable[T])
def tryMap[U, E](f: T => Result[U, E]) =
Result:
s.map(f(_).?)
s.map(f(_).ok)

def getEven(v: Int) =
if v % 2 == 0 then Ok(v) else Err("not even number")
Expand All @@ -25,7 +25,7 @@ class CollectionTest extends munit.FunSuite {
val evens =
(1 to 5)
.map(getEven)
.flatMap(_.ok)
.flatMap(_.toOption)

assertEquals(evens, Seq(2, 4).toIndexedSeq)
}
Expand All @@ -44,6 +44,6 @@ class CollectionTest extends munit.FunSuite {

def tryMap[U, E](f: T => Result[U, E]) =
Result:
t.map(f(_).?)
t.map(f(_).ok)
}
}
18 changes: 9 additions & 9 deletions src/test/scala/result/Result.scala
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,10 @@ class ResultTest extends munit.FunSuite {
}

test("conversions") {
assertEquals(ok.ok, Some(1))
assertEquals(err.ok, None)
assertEquals(ok.err, None)
assertEquals(err.err, Some("bad"))
assertEquals(ok.toOption, Some(1))
assertEquals(err.toOption, None)
assertEquals(ok.errOption, None)
assertEquals(err.errOption, Some("bad"))
assertEquals(ok.toOption, Some(1))
assertEquals(err.toOption, None)

Expand Down Expand Up @@ -158,10 +158,10 @@ class ResultTest extends munit.FunSuite {
Result:
if input < 1 then eval.raise(NoLog)
else if input == 1 then 0
else log2(input / 2).? + 1
else log2(input / 2).ok + 1

Result[Int, Exception]: label ?=>
log2(5).?
log2(5).ok

assertEquals(log2(4), Ok(2))
assertEquals(log2(-1), Err(LogErr.NL(NoLog)))
Expand All @@ -179,7 +179,7 @@ class ResultTest extends munit.FunSuite {
s match
case Seq() => init
case Seq(h, t*) =>
val next = f(init, h).?
val next = f(init, h).ok
eval.break(tryFoldLeft(next, f)(t))
}

Expand All @@ -202,12 +202,12 @@ class ResultTest extends munit.FunSuite {
// Error message tests, uncomment to see.

// test("outside of scope") {
// val x = ok.?
// val x = ok.ok
// }

// test("wrong error type") {
// Result[Int, Int]:
// val x = ok.?
// val x = ok.ok
// val y = eval.raise("a")
// 1
// }
Expand Down

0 comments on commit 83bc101

Please sign in to comment.