Skip to content

Instantly share code, notes, and snippets.

@keynmol
Created September 6, 2024 15:06
Show Gist options
  • Save keynmol/53f03d5be853320f48ba22e5145227a2 to your computer and use it in GitHub Desktop.
Save keynmol/53f03d5be853320f48ba22e5145227a2 to your computer and use it in GitHub Desktop.
Labeled nested breaks in Scala
object lib:
import scala.annotation.implicitNotFound
import scala.util.boundary, boundary.*
opaque type Lab[T <: Singleton] >: Unit = Unit
object Lab:
inline def apply[T <: Singleton]: Lab[T] = ()
inline def label[T <: Singleton & String](
inline f: Label[Lab[T]] ?=> Unit
) = boundary[Lab[T]](f)
inline def breakL[T <: Singleton & String](using
@implicitNotFound("No label [${T}] to break out of")
l: Label[Lab[T]]
) = break(Lab[T])
@main def hello =
import lib.*
label["outer"]:
while true do
println("Entered the outer loop")
label["inner"]:
while true do
println("Entered the inner loop")
breakL["outer"]
println("This point will never be reached")
println("Exited the outer loop")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment