-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(boundaries): add a result boundary
- Loading branch information
Showing
6 changed files
with
63 additions
and
27 deletions.
There are no files selected for viewing
26 changes: 0 additions & 26 deletions
26
src/main/scala/io/github/tassiLuca/boundaries/BOptional.scala
This file was deleted.
Oops, something went wrong.
This file contains 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
22 changes: 22 additions & 0 deletions
22
src/main/scala/io/github/tassiLuca/boundaries/ResultExamples.scala
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package io.github.tassiLuca.boundaries | ||
|
||
import io.github.tassiLuca.boundaries.result.{Ok, Result, Error} | ||
|
||
object ResultExamples extends App: | ||
|
||
type User = String | ||
type Address = String | ||
|
||
def getUser(id: String): Result[User] = | ||
Ok("Mario Rossi") | ||
|
||
def getAddress(user: User): Result[Address] = | ||
Error("The user doesn't exists") | ||
|
||
def together(): Result[String] = | ||
result: | ||
val user = getUser("101").? | ||
val address = getAddress(user).? | ||
user + address | ||
|
||
println(together()) |
12 changes: 12 additions & 0 deletions
12
src/main/scala/io/github/tassiLuca/boundaries/optional.scala
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package io.github.tassiLuca.boundaries | ||
|
||
import scala.util.boundary | ||
import scala.util.boundary.{Label, break} | ||
|
||
object optional: | ||
inline def apply[T](inline body: Label[None.type] ?=> T): Option[T] = | ||
boundary(Some(body)) | ||
|
||
extension [T](o: Option[T]) | ||
inline def ?(using label: Label[None.type]): T = | ||
o.getOrElse(break(None)) |
18 changes: 18 additions & 0 deletions
18
src/main/scala/io/github/tassiLuca/boundaries/result.scala
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package io.github.tassiLuca.boundaries | ||
|
||
import scala.util.boundary | ||
import scala.util.boundary.{Label, break} | ||
|
||
object result: | ||
|
||
sealed trait Result[+T] | ||
case class Ok[+T](t: T) extends Result[T] | ||
case class Error(e: String) extends Result[Nothing] | ||
|
||
inline def apply[T](inline body: Label[Error] ?=> T): Result[T] = | ||
boundary(Ok(body)) | ||
|
||
extension [T](r: Result[T]) | ||
inline def ?(using Label[Error]): T = r match | ||
case Ok(t) => t | ||
case e @ Error(_) => break(e) |
This file contains 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