Skip to content

Instantly share code, notes, and snippets.

@carlosedp
Last active November 10, 2023 16:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carlosedp/c65b80ac1c0ddecd266f49248916a3df to your computer and use it in GitHub Desktop.
Save carlosedp/c65b80ac1c0ddecd266f49248916a3df to your computer and use it in GitHub Desktop.
Direct tuple assignment in for comprehension
// 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