Created
September 6, 2024 15:06
-
-
Save keynmol/53f03d5be853320f48ba22e5145227a2 to your computer and use it in GitHub Desktop.
Labeled nested breaks in Scala
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
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