More idiomatic Result API - #42
Merged
Merged
Conversation
Extension methods are still potentially causing colisions, so lets avoid in most cases. - Move all methods from companion extension blocks into the enum body, using stdlib-style lower bounds (getOrElse[T1 >: T], flatMap[U, E1 >: E], etc.) and evidence-based flatten/toTry like Either's. The sources move from package.scala to Result.scala. - Rename `or` to `orElse` and `getNullable` to `orNull` (with Option.orNull's signature); tap/tapErr now take f: T => U per the existing TODOs. - Keep extensions only where required: Result.eval (unchanged), the right-associative *: operator, and ScalaConverters on stdlib types (which would become real methods if merged into the stdlib). - Remove the unidiomatic ResultIsErrException case class; get now throws a plain NoSuchElementException like Option.get. - Add Result.catching to construct a Result from a NonFatal-throwing body, with tests covering success, caught, and fatal rethrow cases.
natsukagami
approved these changes
Jul 29, 2026
| * [[Err]]. | ||
| * @group access | ||
| */ | ||
| def orNull[T1 >: T](using ev: Null <:< T1): T1 = this match |
Contributor
There was a problem hiding this comment.
How would inference deal with this? 🤔 it looks complicated, most of the time I just want T | Null, but is it not possible because of variance?
Member
Author
There was a problem hiding this comment.
I lifted the signature from Option, which infers like this:
scala> Some(1).orNull
val res0: Int | Null = 1
Member
Author
There was a problem hiding this comment.
a type variable does give the option to infer a nicer type as well - e.g. for getOrElse it could infer the sealed trait type rather than a straight union of value | default
scala> def getOrElse2[A, B](a: Option[A])(b: => B): A | B = a match
case Some(a1) => a1
case _ => b
def getOrElse2[A, B](a: Option[A])(b: => B): A | B
scala> sealed trait Bar; case class X() extends Bar; case class Y() extends Bar
// defined trait Bar
// defined case class X
// defined case class Y
scala> Some(X()).getOrElse(Y())
val res4: Bar = X() // type variable picks a nicer target
scala> getOrElse2(Some(X()))(Y())
val res5: X | Y = X() // union is too precise
Member
Author
There was a problem hiding this comment.
Just seen that Option adopted a new signature, so lets follow that:
final def orNull[A1 >: A | Null]: A1 = ???
Contributor
There was a problem hiding this comment.
Sounds good :) I think we should also compile with strict nulls
bishabosha
force-pushed
the
more-idiomatic
branch
from
August 13, 2026 08:25
b64e3df to
4381d50
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
In preparation to make Result stable for standard library: