Last active
November 10, 2023 16:42
-
-
Save carlosedp/c65b80ac1c0ddecd266f49248916a3df to your computer and use it in GitHub Desktop.
Direct tuple assignment in for comprehension
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
// Run with scala-cli TupleAssing.scala | |
// | |
// //> using scala "3.3.1" | |
//> using scala "3.4.0-RC1-bin-20231109-c7b3d7b-NIGHTLY" | |
//> using lib "dev.zio::zio:2.0.19" | |
// (required before Scala 3.4) | |
// //> using options -source:future | |
import zio.* | |
@main | |
def main() = | |
// Pure scala: | |
def x = | |
for | |
(a, b) <- Some(1, 2) | |
yield (a, b) | |
println(x.get) | |
// With ZIO unpacking separately (works): | |
def y = | |
for | |
x <- ZIO.fromOption(Some(1, 2)) | |
(a, b) = x | |
yield (a, b) | |
println(runZIO(y)) | |
// With ZIO unpacking directly (doesn't work unless using "-source:future"): | |
def z = | |
for | |
(a, b) <- ZIO.fromOption(Some(1, 2)) | |
yield (a, b) | |
println(runZIO(z)) | |
end main | |
// Helper to run ZIO effects: | |
def runZIO(eff: ZIO[Any, Any, Any]): Any = | |
Unsafe.unsafe { implicit unsafe => | |
Runtime.default.unsafe.run(eff).getOrThrowFiberFailure() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment