continuations4s
Continuations4s
Low-level, platform-specific, delimited continuations, implemented for Scala 3, with support for JVM, ScalaJS, and Scala Native. Needed to support cross-platform, "direct style" libraries.
Implementation notes:
- Scala Native has baked-in delimited continuations support;
- ScalaJS, using the WASM backend, can use WASM JSPI;
- On the JVM the implementation blocks threads, which is cheaper to do since Java 21's virtual threads.
Extracted from the lampepfl/gears project.
Usage
libraryDependencies += "org.funfix" %%% "continuations4s" % "x.y.z"
Example — this works across JVM, ScalaJS (WASM) and Scala Native:
import continuations4s.Continuations.*
import scala.concurrent.Future
def await[T, R](future: Future[T])(using Label[Unit]): T = {
val result = suspend[Try[T], Unit] { sus =>
future.onComplete { value =>
sus.resume(value)
}
}
result.get
}
def runToFuture[T](body: Label[Unit] ?=> T): Future[T] = {
val p = Promise[T]()
val _ = boundary[Unit] {
val r = body
p.success(r)
}
p.future
}
val result: Future[Int] =
runToFuture {
val f1 = Future(1 + 1) // executed async
val r1 = await(f1) // blocks for the result
val f2 = Future(10) // executed async
val r2 = await(f2) // blocks for the result
r1 + r2
}
License
Copyright 2024-2026 LAMP, EPFL. Copyright 2026 Alexandru Nedelcu.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.