SlideShare a Scribd company logo
1 of 44
Download to read offline
Direct Style Scala
Martin Odersky
EPFL
Scalar Conference
March 24, 2023
Shifting Foundations
Trends
Widespread support for async/await
Runtimes get better support for fibers or continuations.
Examples
Goroutines,
Project Loom in Java,
Kotlin coroutines,
OCaml or Haskell delimited continuations,
Research languages such as Effekt, Koka
Thesis of this talk
This will deeply influence libraries and frameworks
It’s very attractive now to go back to direct style.
Shifting Foundations
Trends
Widespread support for async/await
Runtimes get better support for fibers or continuations.
Examples
Goroutines,
Project Loom in Java,
Kotlin coroutines,
OCaml or Haskell delimited continuations,
Research languages such as Effekt, Koka
Thesis of this talk
This will deeply influence libraries and frameworks
It’s very attractive now to go back to direct style.
How will this influence Scala in the future?
1 There will likely be native foundations for direct-style reactive
programming
Delimited continuations on Scala Native
Fibers on latest Java
Source or bytecode rewriting for older Java, JS
2 This will enable new techniques for designing and composing
software
3 There will be a move away from monads as the primary way
of code composition.
Building a Direct-Style Stack
First step: Boundary/break
Error handling
Suspensions
Concurrency library design built on that
Building a Direct-Style Stack
First step: Boundary/break
(shipped)
Error handling
(enabled)
Suspensions
(wip)
Concurrency library design built on that
(wip)
Warmup: Boundary/break
A cleaner alternative to non-local returns (which will go away)
def firstIndex[T](xs: List[T], elem: T): Int =
boundary:
for (x, i) <- xs.zipWithIndex do
if x == elem then break(i)
-1
boundary establishes a boundary
break returns with a value from it.
Stack View
API
package scala.util
object boundary:
final class Label[-T]
def break[T](value: T)(using label: Label[T]): Nothing =
throw Break(label, value)
inline def apply[T](inline body: Label[T] ?=> T): T = ...
end boundary
To break, you need a label that represents the boundary.
In a sense, label is a capability that enables to break.
(This is a common pattern)
Implementation
The implementation of break produces efficient code.
If break appears in the same stackframe as its boundary, use a
jump.
Otherwise use a fast exception that does not capture a stack
trace.
A stack trace is not needed since we know the exception will be
handled (*)
(*) To be 100% sure, this needs capture checking.
Implementation
The implementation of break produces efficient code.
If break appears in the same stackframe as its boundary, use a
jump.
Otherwise use a fast exception that does not capture a stack
trace.
A stack trace is not needed since we know the exception will be
handled (*)
(*) To be 100% sure, this needs capture checking.
Stage 2: Error handling
boundary/break can be used as the basis for flexible error handling.
For instance:
def firstColumn[T](xss: List[List[T]]): Option[List[T]] =
optional:
xss.map(_.headOption.?)
Optionally, returns the first column of the matrix xss.
Returns None if there is an empty row.
Error handling implementation
optional and ? on options can be implemented quite easily on top
of boundary/break:
object optional:
inline def apply[T](inline body: Label[None.type] ?=> T)
: Option[T] = boundary(Some(body))
extension [T](r: Option[T])
inline def ? (using label: Label[None.type]): T = r match
case Some(x) => x
case None => break(None)
Analogous implementations are possible for other result types such
as Either or a Rust-like Result.
My ideal way of error handling would be based on Result + ?.
Stage 3: Suspensions
Question: What if we could store the stack segment between a
break and its boundary and re-use it at some later time?
Suspensions
Question: What if we could store the stack segment between a
break and its boundary and re-use it at some later time?
This is the idea of delimited continuations.
Suspensions
Question: What if we could store the stack segment between a
break and its boundary and re-use it at some later time?
This is the idea of delimited continuations.
Suspension API
class Suspension[-T, +R]:
def resume(arg: T): R = ???
def suspend[T, R](body: Suspension[T, R] => R)(using Label[R]): T
Suspensions are quite powerful.
They can express at the same time algebraic effects and monads.
Generators
Python-style generators are a simple example of algebraic effects.
def example = Generator:
produce(”We’ll give you all the numbers divisible by 3 or 2”)
for i <- 1 to 1000 do
if i % 3 == 0 then
produce(s”$i is divisible by 3”)
else if i % 2 == 0 then
produce(s”$i is even”)
Here, Generator is essentially a simplified Iterator
trait Generator[T]:
def nextOption: Option[T]
Algebraic Effects
Task: Build a generate implementation of Generator, so that one
can compute the leafs of a Tree like this:
enum Tree[T]:
case Leaf(x: T)
case Inner(xs: List[Tree[T]])
def leafs[T](t: Tree[T]): Generator[T] =
generate: // effect scope
def recur(t: Tree[T]): Unit = t match
case Tree.Leaf(x) => produce(x) // effect
case Tree.Inner(xs) => xs.foreach(recur)
recur(t)
Generator Implementation
trait Produce[-T]:
def produce(x: T): Unit
def generate[T](body: Produce[T] ?=> Unit) = new Generator[T]:
def nextOption: Option[T] = step()
var step: () => Option[T] =
The Step Function
trait Produce[-T]: // effect type
def produce(x: T): Unit
def generate[T](body: Produce[T] ?=> Unit) = new Generator[T]:
def nextOption: Option[T] = step()
var step: () => Option[T] = () =>
boundary:
given Produce[T] with // handler
def produce(x: T): Unit =
suspend[Unit, Option[T]]: k =>
step = () => k.resume(())
Some(x)
body
None
Summary: Algebraic Effects
Effects are methods of effect traits
Handlers are implementations of effect traits
They are passed as implicit parameters.
They can abort part of a computation via break
They can also suspend part of a computation as a
continuation and resume it later.
Implementing Suspensions
There are several possibilities:
Directly in the runtime, as shown in the designs
On top of fibers (requires some compromises)
By bytecode rewriting (e.g. Quasar, javactrl)
By source rewriting
Suspensions and Monads:
Wadler (1993): Continuations can be expressed as a monad.
“Haskell is the essence of ML”
Filinski (1994): Every monad can be expressed in direct style
using just delimited continuations.
“ML is the essence of Haskell”
My take: designs based on continuations are simpler to compose
than monads.
Suspensions and Monads:
Wadler (1993): Continuations can be expressed as a monad.
“Haskell is the essence of ML”
Filinski (1994): Every monad can be expressed in direct style
using just delimited continuations.
“ML is the essence of Haskell”
My take: designs based on continuations are simpler to compose
than monads.
Direct-Style Futures
With suspend(*), we can implement lightweight and universal
await construct that can be called anywhere.
This can express simple, direct-style futures.
val sum = Future:
val f1 = Future(c1.read)
val f2 = Future(c2.read)
f1.value + f2.value
Structured concurrency: Local futures f1 and f2 complete before
sum completes. This might mean that one of them is cancelled if
the other returns with a failure.
(*) Loom-like fibers would work as well.
Compare with Status Quo
val sum =
val f1 = Future(c1.read)
val f2 = Future(c2.read)
for
x <- f1
y <- f2
yield x + y
Composition of futures is monadic
but creation isn’t, which is a bit awkward.
A Strawman
lampepfl/async is an early stage prototype of a modern, low-level
concurrency library in direct style.
Main elements
Futures: the primary active elements
Channels: the primary passive elements
Async Sources Futures and Channels both implement a new
fundamental abstraction: an asynchronous source.
Async Contexts An async context is a capability that allows
a computation to suspend while waiting for the result of an
async source.
Link: github.com/lampepfl/async
Futures
The Future trait is defined as follows:
trait Future[+T] extends Async.Source[Try[T]], Cancellable:
def result(using Async): Try[T]
def value(using Async): T = result.get
The result method can be defined like this:
def result(using async: Async): T = async.await(this)
async is a capability that allows to suspend in an await method.
Futures
The Future trait is defined as follows:
trait Future[+T] extends Async.Source[Try[T]], Cancellable:
def result(using Async): Try[T]
def value(using Async): T = result.get
The result method can be defined like this:
def result(using async: Async): T = async.await(this)
async is a capability that allows to suspend in an await method.
Async
The Async trait is defined as follows:
trait Async:
def await[T](src: Async.Source[T]): T
def scheduler: ExecutionContext
def group: CancellationGroup
def withGroup(group: CancellationGroup): Async
await gets the (first) element of an Async.Source.
It suspends if necessary.
Async.Source
Futures are a particular kind of an async source. (Other
implementations come from channels).
Async sources are the primary means of communication
between asynchronous computations
They can be composed in interesting ways.
For instance, map and filter are provided:
extension [T](s: Source[T])
def map[U](f: T => U): Source[U]
def filter(p: T => Boolean): Source[T]
Async.Source
Futures are a particular kind of an async source. (Other
implementations come from channels).
Async sources are the primary means of communication
between asynchronous computations
They can be composed in interesting ways.
For instance, map and filter are provided:
extension [T](s: Source[T])
def map[U](f: T => U): Source[U]
def filter(p: T => Boolean): Source[T]
Races
A race passes on the first of several sources:
def race[T](sources: Source[T]*): Source[T]
Higher-level operation:
def either[T1, T2](src1: Source[T1], src2: Source[T2])
: Source[Either[T, U]] =
race(
src1.map(Left(_)),
src2.map(Right(_))
)
Structured Concurrency
It’s now easy to implement zip and alt on futures:
extension [T](f1: Future[T])
def zip[U](f2: Future[U])(using Async): Future[(T, U)] =
Future:
await(either(f1, f2)) match
case Left(Success(x1)) => (x1, f2.value)
case Right(Success(x2)) => (f1.value, x2)
case Left(Failure(ex)) => throw ex
case Right(Failure(ex)) => throw ex
Structured Concurrency
It’s now easy to implement zip and alt on futures:
extension [T](f1: Future[T])
def alt(f2: Future[T])(using Async): Future[T] =
Future:
await(either(f1, f2)) match
case Left(Success(x1)) => x1
case Right(Success(x2)) => x2
case Left(_: Failure[?]) => f2.value
case Right(_: Failure[?]) => f1.value
Why Futures & Channels?
Futures: The simplest way to get parallelism
Define a computation
Run it in parallel
Await the result when needed
Channels: The canonical way of communication between
computations.
Both are instances as asynchronous sources
Why not Coroutines?
Often, coroutines (in the sense of CSP or goroutines) are used
instead of futures to work with channels.
But:
We need to be able to wait for a coroutine’s termination.
We need to handle any exceptions in the coroutine on the
outside
Both are achieved by using a Future[Unit].
So no different abstractions are needed.
Why an ErrorType Fixed to Try?
Natural solution if the language supports exception
But common complaint for current futures:
Error type is fixed to be Exception.
This makes it awkward to handle other errors.
For instance, how would you implement this function?
def acrobatics(xs: List[Future[Result[T, E]]])
: Future[Result[List[T], E]] =
Why an ErrorType Fixed to Try?
Natural solution if the language supports exception
But common complaint for current futures:
Error type is fixed to be Exception.
This makes it awkward to handle other errors.
For instance, how would you implement this function?
def acrobatics(xs: List[Future[Result[T, E]]])
: Future[Result[List[T], E]] =
Why an ErrorType Fixed to Try?
Natural solution if the language supports exception
But common complaint for current futures:
Error type is fixed to be Exception.
This makes it awkward to handle other errors.
For instance, how would you implement this function?
def acrobatics(xs: List[Future[Result[T, E]]])
: Future[Result[List[T], E]] =
Why an ErrorType Fixed to Try?
Natural solution if the language supports exception
But common complaint for current futures:
Error type is fixed to be Exception.
This makes it awkward to handle other errors.
New direct style abstractions don’t have that problem anymore!
def acrobatics(xs: List[Future[Result[T, E]]])
: Future[Result[List[T], E]] =
Future:
Result:
xs.map(_.value.?)
Simple compositions, no traverse or lift is needed.
Conclusion
Direct style has lots to offer
Suspensions can express every monad, but, provide more flexible
composition.
This gives completely new possibilities to express practical
foundations for concurrency and async I/O.
The future will be interesting…
Thank You
Conclusion
Direct style has lots to offer
Suspensions can express every monad, but, provide more flexible
composition.
This gives completely new possibilities to express practical
foundations for concurrency and async I/O.
The future will be interesting…
Thank You

More Related Content

What's hot

Functional Core and Imperative Shell - Game of Life Example - Haskell and Scala
Functional Core and Imperative Shell - Game of Life Example - Haskell and ScalaFunctional Core and Imperative Shell - Game of Life Example - Haskell and Scala
Functional Core and Imperative Shell - Game of Life Example - Haskell and ScalaPhilip Schwarz
 
The Functional Programming Triad of Map, Filter and Fold
The Functional Programming Triad of Map, Filter and FoldThe Functional Programming Triad of Map, Filter and Fold
The Functional Programming Triad of Map, Filter and FoldPhilip Schwarz
 
N-Queens Combinatorial Problem - Polyglot FP for Fun and Profit – Haskell and...
N-Queens Combinatorial Problem - Polyglot FP for Fun and Profit – Haskell and...N-Queens Combinatorial Problem - Polyglot FP for Fun and Profit – Haskell and...
N-Queens Combinatorial Problem - Polyglot FP for Fun and Profit – Haskell and...Philip Schwarz
 
Sequence and Traverse - Part 2
Sequence and Traverse - Part 2Sequence and Traverse - Part 2
Sequence and Traverse - Part 2Philip Schwarz
 
Functional Domain Modeling - The ZIO 2 Way
Functional Domain Modeling - The ZIO 2 WayFunctional Domain Modeling - The ZIO 2 Way
Functional Domain Modeling - The ZIO 2 WayDebasish Ghosh
 
Peeking inside the engine of ZIO SQL.pdf
Peeking inside the engine of ZIO SQL.pdfPeeking inside the engine of ZIO SQL.pdf
Peeking inside the engine of ZIO SQL.pdfJaroslavRegec1
 
Http4s, Doobie and Circe: The Functional Web Stack
Http4s, Doobie and Circe: The Functional Web StackHttp4s, Doobie and Circe: The Functional Web Stack
Http4s, Doobie and Circe: The Functional Web StackGaryCoady
 
The aggregate function - from sequential and parallel folds to parallel aggre...
The aggregate function - from sequential and parallel folds to parallel aggre...The aggregate function - from sequential and parallel folds to parallel aggre...
The aggregate function - from sequential and parallel folds to parallel aggre...Philip Schwarz
 
Left and Right Folds - Comparison of a mathematical definition and a programm...
Left and Right Folds- Comparison of a mathematical definition and a programm...Left and Right Folds- Comparison of a mathematical definition and a programm...
Left and Right Folds - Comparison of a mathematical definition and a programm...Philip Schwarz
 
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part ...
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part ...Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part ...
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part ...Philip Schwarz
 
Boost your productivity with Scala tooling!
Boost your productivity  with Scala tooling!Boost your productivity  with Scala tooling!
Boost your productivity with Scala tooling!MeriamLachkar1
 
A Prelude of Purity: Scaling Back ZIO
A Prelude of Purity: Scaling Back ZIOA Prelude of Purity: Scaling Back ZIO
A Prelude of Purity: Scaling Back ZIOJorge Vásquez
 
Exploring ZIO Prelude: The game changer for typeclasses in Scala
Exploring ZIO Prelude: The game changer for typeclasses in ScalaExploring ZIO Prelude: The game changer for typeclasses in Scala
Exploring ZIO Prelude: The game changer for typeclasses in ScalaJorge Vásquez
 
Nat, List and Option Monoids - from scratch - Combining and Folding - an example
Nat, List and Option Monoids -from scratch -Combining and Folding -an exampleNat, List and Option Monoids -from scratch -Combining and Folding -an example
Nat, List and Option Monoids - from scratch - Combining and Folding - an examplePhilip Schwarz
 
Ad hoc Polymorphism using Type Classes and Cats
Ad hoc Polymorphism using Type Classes and CatsAd hoc Polymorphism using Type Classes and Cats
Ad hoc Polymorphism using Type Classes and CatsPhilip Schwarz
 
Composing an App with Free Monads (using Cats)
Composing an App with Free Monads (using Cats)Composing an App with Free Monads (using Cats)
Composing an App with Free Monads (using Cats)Hermann Hueck
 
Sum and Product Types - The Fruit Salad & Fruit Snack Example - From F# to Ha...
Sum and Product Types -The Fruit Salad & Fruit Snack Example - From F# to Ha...Sum and Product Types -The Fruit Salad & Fruit Snack Example - From F# to Ha...
Sum and Product Types - The Fruit Salad & Fruit Snack Example - From F# to Ha...Philip Schwarz
 
Quill vs Slick Smackdown
Quill vs Slick SmackdownQuill vs Slick Smackdown
Quill vs Slick SmackdownAlexander Ioffe
 

What's hot (20)

Functional Core and Imperative Shell - Game of Life Example - Haskell and Scala
Functional Core and Imperative Shell - Game of Life Example - Haskell and ScalaFunctional Core and Imperative Shell - Game of Life Example - Haskell and Scala
Functional Core and Imperative Shell - Game of Life Example - Haskell and Scala
 
The Functional Programming Triad of Map, Filter and Fold
The Functional Programming Triad of Map, Filter and FoldThe Functional Programming Triad of Map, Filter and Fold
The Functional Programming Triad of Map, Filter and Fold
 
N-Queens Combinatorial Problem - Polyglot FP for Fun and Profit – Haskell and...
N-Queens Combinatorial Problem - Polyglot FP for Fun and Profit – Haskell and...N-Queens Combinatorial Problem - Polyglot FP for Fun and Profit – Haskell and...
N-Queens Combinatorial Problem - Polyglot FP for Fun and Profit – Haskell and...
 
Sequence and Traverse - Part 2
Sequence and Traverse - Part 2Sequence and Traverse - Part 2
Sequence and Traverse - Part 2
 
Functional Domain Modeling - The ZIO 2 Way
Functional Domain Modeling - The ZIO 2 WayFunctional Domain Modeling - The ZIO 2 Way
Functional Domain Modeling - The ZIO 2 Way
 
Peeking inside the engine of ZIO SQL.pdf
Peeking inside the engine of ZIO SQL.pdfPeeking inside the engine of ZIO SQL.pdf
Peeking inside the engine of ZIO SQL.pdf
 
Http4s, Doobie and Circe: The Functional Web Stack
Http4s, Doobie and Circe: The Functional Web StackHttp4s, Doobie and Circe: The Functional Web Stack
Http4s, Doobie and Circe: The Functional Web Stack
 
The aggregate function - from sequential and parallel folds to parallel aggre...
The aggregate function - from sequential and parallel folds to parallel aggre...The aggregate function - from sequential and parallel folds to parallel aggre...
The aggregate function - from sequential and parallel folds to parallel aggre...
 
Left and Right Folds - Comparison of a mathematical definition and a programm...
Left and Right Folds- Comparison of a mathematical definition and a programm...Left and Right Folds- Comparison of a mathematical definition and a programm...
Left and Right Folds - Comparison of a mathematical definition and a programm...
 
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part ...
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part ...Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part ...
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part ...
 
Applicative Functor
Applicative FunctorApplicative Functor
Applicative Functor
 
Boost your productivity with Scala tooling!
Boost your productivity  with Scala tooling!Boost your productivity  with Scala tooling!
Boost your productivity with Scala tooling!
 
A Prelude of Purity: Scaling Back ZIO
A Prelude of Purity: Scaling Back ZIOA Prelude of Purity: Scaling Back ZIO
A Prelude of Purity: Scaling Back ZIO
 
Exploring ZIO Prelude: The game changer for typeclasses in Scala
Exploring ZIO Prelude: The game changer for typeclasses in ScalaExploring ZIO Prelude: The game changer for typeclasses in Scala
Exploring ZIO Prelude: The game changer for typeclasses in Scala
 
Nat, List and Option Monoids - from scratch - Combining and Folding - an example
Nat, List and Option Monoids -from scratch -Combining and Folding -an exampleNat, List and Option Monoids -from scratch -Combining and Folding -an example
Nat, List and Option Monoids - from scratch - Combining and Folding - an example
 
Ad hoc Polymorphism using Type Classes and Cats
Ad hoc Polymorphism using Type Classes and CatsAd hoc Polymorphism using Type Classes and Cats
Ad hoc Polymorphism using Type Classes and Cats
 
Composing an App with Free Monads (using Cats)
Composing an App with Free Monads (using Cats)Composing an App with Free Monads (using Cats)
Composing an App with Free Monads (using Cats)
 
Python-DataAbstarction.pptx
Python-DataAbstarction.pptxPython-DataAbstarction.pptx
Python-DataAbstarction.pptx
 
Sum and Product Types - The Fruit Salad & Fruit Snack Example - From F# to Ha...
Sum and Product Types -The Fruit Salad & Fruit Snack Example - From F# to Ha...Sum and Product Types -The Fruit Salad & Fruit Snack Example - From F# to Ha...
Sum and Product Types - The Fruit Salad & Fruit Snack Example - From F# to Ha...
 
Quill vs Slick Smackdown
Quill vs Slick SmackdownQuill vs Slick Smackdown
Quill vs Slick Smackdown
 

Similar to scalar.pdf

scala.reflect, Eugene Burmako
scala.reflect, Eugene Burmakoscala.reflect, Eugene Burmako
scala.reflect, Eugene BurmakoVasil Remeniuk
 
Евгений Бурмако «scala.reflect»
Евгений Бурмако «scala.reflect»Евгений Бурмако «scala.reflect»
Евгений Бурмако «scala.reflect»e-Legion
 
A Survey of Concurrency Constructs
A Survey of Concurrency ConstructsA Survey of Concurrency Constructs
A Survey of Concurrency ConstructsTed Leung
 
Scala Talk at FOSDEM 2009
Scala Talk at FOSDEM 2009Scala Talk at FOSDEM 2009
Scala Talk at FOSDEM 2009Martin Odersky
 
Metaprogramming in Scala 2.10, Eugene Burmako,
Metaprogramming  in Scala 2.10, Eugene Burmako, Metaprogramming  in Scala 2.10, Eugene Burmako,
Metaprogramming in Scala 2.10, Eugene Burmako, Vasil Remeniuk
 
Lecture 5 interface.pdf
Lecture  5 interface.pdfLecture  5 interface.pdf
Lecture 5 interface.pdfAdilAijaz3
 
Os Reindersfinal
Os ReindersfinalOs Reindersfinal
Os Reindersfinaloscon2007
 
Os Reindersfinal
Os ReindersfinalOs Reindersfinal
Os Reindersfinaloscon2007
 
Abstract Data Types (a) Explain briefly what is meant by the ter.pdf
Abstract Data Types (a) Explain briefly what is meant by the ter.pdfAbstract Data Types (a) Explain briefly what is meant by the ter.pdf
Abstract Data Types (a) Explain briefly what is meant by the ter.pdfkarymadelaneyrenne19
 
ParaSail
ParaSail  ParaSail
ParaSail AdaCore
 
Fusing Transformations of Strict Scala Collections with Views
Fusing Transformations of Strict Scala Collections with ViewsFusing Transformations of Strict Scala Collections with Views
Fusing Transformations of Strict Scala Collections with ViewsPhilip Schwarz
 
Scala for Machine Learning
Scala for Machine LearningScala for Machine Learning
Scala for Machine LearningPatrick Nicolas
 
Patterns in Python
Patterns in PythonPatterns in Python
Patterns in Pythondn
 
Guaranteeing Memory Safety in Rust
Guaranteeing Memory Safety in RustGuaranteeing Memory Safety in Rust
Guaranteeing Memory Safety in Rustnikomatsakis
 
Fuel Up JavaScript with Functional Programming
Fuel Up JavaScript with Functional ProgrammingFuel Up JavaScript with Functional Programming
Fuel Up JavaScript with Functional ProgrammingShine Xavier
 

Similar to scalar.pdf (20)

scala.reflect, Eugene Burmako
scala.reflect, Eugene Burmakoscala.reflect, Eugene Burmako
scala.reflect, Eugene Burmako
 
Евгений Бурмако «scala.reflect»
Евгений Бурмако «scala.reflect»Евгений Бурмако «scala.reflect»
Евгений Бурмако «scala.reflect»
 
A Survey of Concurrency Constructs
A Survey of Concurrency ConstructsA Survey of Concurrency Constructs
A Survey of Concurrency Constructs
 
Scala Talk at FOSDEM 2009
Scala Talk at FOSDEM 2009Scala Talk at FOSDEM 2009
Scala Talk at FOSDEM 2009
 
Loops_in_Rv1.2b
Loops_in_Rv1.2bLoops_in_Rv1.2b
Loops_in_Rv1.2b
 
Metaprogramming in Scala 2.10, Eugene Burmako,
Metaprogramming  in Scala 2.10, Eugene Burmako, Metaprogramming  in Scala 2.10, Eugene Burmako,
Metaprogramming in Scala 2.10, Eugene Burmako,
 
Swift, swiftly
Swift, swiftlySwift, swiftly
Swift, swiftly
 
Lecture 5 interface.pdf
Lecture  5 interface.pdfLecture  5 interface.pdf
Lecture 5 interface.pdf
 
Os Reindersfinal
Os ReindersfinalOs Reindersfinal
Os Reindersfinal
 
Os Reindersfinal
Os ReindersfinalOs Reindersfinal
Os Reindersfinal
 
Abstract Data Types (a) Explain briefly what is meant by the ter.pdf
Abstract Data Types (a) Explain briefly what is meant by the ter.pdfAbstract Data Types (a) Explain briefly what is meant by the ter.pdf
Abstract Data Types (a) Explain briefly what is meant by the ter.pdf
 
Devoxx
DevoxxDevoxx
Devoxx
 
ParaSail
ParaSail  ParaSail
ParaSail
 
Matlab Manual
Matlab ManualMatlab Manual
Matlab Manual
 
Fusing Transformations of Strict Scala Collections with Views
Fusing Transformations of Strict Scala Collections with ViewsFusing Transformations of Strict Scala Collections with Views
Fusing Transformations of Strict Scala Collections with Views
 
Scala for Machine Learning
Scala for Machine LearningScala for Machine Learning
Scala for Machine Learning
 
Patterns in Python
Patterns in PythonPatterns in Python
Patterns in Python
 
Intro to Scala
 Intro to Scala Intro to Scala
Intro to Scala
 
Guaranteeing Memory Safety in Rust
Guaranteeing Memory Safety in RustGuaranteeing Memory Safety in Rust
Guaranteeing Memory Safety in Rust
 
Fuel Up JavaScript with Functional Programming
Fuel Up JavaScript with Functional ProgrammingFuel Up JavaScript with Functional Programming
Fuel Up JavaScript with Functional Programming
 

More from Martin Odersky

What To Leave Implicit
What To Leave ImplicitWhat To Leave Implicit
What To Leave ImplicitMartin Odersky
 
What To Leave Implicit
What To Leave ImplicitWhat To Leave Implicit
What To Leave ImplicitMartin Odersky
 
Implementing Higher-Kinded Types in Dotty
Implementing Higher-Kinded Types in DottyImplementing Higher-Kinded Types in Dotty
Implementing Higher-Kinded Types in DottyMartin Odersky
 
Compilers Are Databases
Compilers Are DatabasesCompilers Are Databases
Compilers Are DatabasesMartin Odersky
 
Scala Days San Francisco
Scala Days San FranciscoScala Days San Francisco
Scala Days San FranciscoMartin Odersky
 
The Evolution of Scala
The Evolution of ScalaThe Evolution of Scala
The Evolution of ScalaMartin Odersky
 
Scala - The Simple Parts, SFScala presentation
Scala - The Simple Parts, SFScala presentationScala - The Simple Parts, SFScala presentation
Scala - The Simple Parts, SFScala presentationMartin Odersky
 
flatMap Oslo presentation slides
flatMap Oslo presentation slidesflatMap Oslo presentation slides
flatMap Oslo presentation slidesMartin Odersky
 
Oscon keynote: Working hard to keep it simple
Oscon keynote: Working hard to keep it simpleOscon keynote: Working hard to keep it simple
Oscon keynote: Working hard to keep it simpleMartin Odersky
 
Scala eXchange opening
Scala eXchange openingScala eXchange opening
Scala eXchange openingMartin Odersky
 

More from Martin Odersky (15)

Simplicitly
SimplicitlySimplicitly
Simplicitly
 
What To Leave Implicit
What To Leave ImplicitWhat To Leave Implicit
What To Leave Implicit
 
What To Leave Implicit
What To Leave ImplicitWhat To Leave Implicit
What To Leave Implicit
 
From DOT to Dotty
From DOT to DottyFrom DOT to Dotty
From DOT to Dotty
 
Implementing Higher-Kinded Types in Dotty
Implementing Higher-Kinded Types in DottyImplementing Higher-Kinded Types in Dotty
Implementing Higher-Kinded Types in Dotty
 
Scala Days NYC 2016
Scala Days NYC 2016Scala Days NYC 2016
Scala Days NYC 2016
 
Compilers Are Databases
Compilers Are DatabasesCompilers Are Databases
Compilers Are Databases
 
Scala Days San Francisco
Scala Days San FranciscoScala Days San Francisco
Scala Days San Francisco
 
Scalax
ScalaxScalax
Scalax
 
The Evolution of Scala
The Evolution of ScalaThe Evolution of Scala
The Evolution of Scala
 
Scala - The Simple Parts, SFScala presentation
Scala - The Simple Parts, SFScala presentationScala - The Simple Parts, SFScala presentation
Scala - The Simple Parts, SFScala presentation
 
Flatmap
FlatmapFlatmap
Flatmap
 
flatMap Oslo presentation slides
flatMap Oslo presentation slidesflatMap Oslo presentation slides
flatMap Oslo presentation slides
 
Oscon keynote: Working hard to keep it simple
Oscon keynote: Working hard to keep it simpleOscon keynote: Working hard to keep it simple
Oscon keynote: Working hard to keep it simple
 
Scala eXchange opening
Scala eXchange openingScala eXchange opening
Scala eXchange opening
 

Recently uploaded

Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityNeo4j
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationkaushalgiri8080
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio, Inc.
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - InfographicHr365.us smith
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number SystemsJheuzeDellosa
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...Christina Lin
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...gurkirankumar98700
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...aditisharan08
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptkotipi9215
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideChristina Lin
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyFrank van der Linden
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 

Recently uploaded (20)

Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered Sustainability
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanation
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - Infographic
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number Systems
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
Exploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the ProcessExploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the Process
 
Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.ppt
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The Ugly
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 

scalar.pdf

  • 1. Direct Style Scala Martin Odersky EPFL Scalar Conference March 24, 2023
  • 2. Shifting Foundations Trends Widespread support for async/await Runtimes get better support for fibers or continuations. Examples Goroutines, Project Loom in Java, Kotlin coroutines, OCaml or Haskell delimited continuations, Research languages such as Effekt, Koka Thesis of this talk This will deeply influence libraries and frameworks It’s very attractive now to go back to direct style.
  • 3. Shifting Foundations Trends Widespread support for async/await Runtimes get better support for fibers or continuations. Examples Goroutines, Project Loom in Java, Kotlin coroutines, OCaml or Haskell delimited continuations, Research languages such as Effekt, Koka Thesis of this talk This will deeply influence libraries and frameworks It’s very attractive now to go back to direct style.
  • 4. How will this influence Scala in the future? 1 There will likely be native foundations for direct-style reactive programming Delimited continuations on Scala Native Fibers on latest Java Source or bytecode rewriting for older Java, JS 2 This will enable new techniques for designing and composing software 3 There will be a move away from monads as the primary way of code composition.
  • 5. Building a Direct-Style Stack First step: Boundary/break Error handling Suspensions Concurrency library design built on that
  • 6. Building a Direct-Style Stack First step: Boundary/break (shipped) Error handling (enabled) Suspensions (wip) Concurrency library design built on that (wip)
  • 7. Warmup: Boundary/break A cleaner alternative to non-local returns (which will go away) def firstIndex[T](xs: List[T], elem: T): Int = boundary: for (x, i) <- xs.zipWithIndex do if x == elem then break(i) -1 boundary establishes a boundary break returns with a value from it.
  • 9. API package scala.util object boundary: final class Label[-T] def break[T](value: T)(using label: Label[T]): Nothing = throw Break(label, value) inline def apply[T](inline body: Label[T] ?=> T): T = ... end boundary To break, you need a label that represents the boundary. In a sense, label is a capability that enables to break. (This is a common pattern)
  • 10. Implementation The implementation of break produces efficient code. If break appears in the same stackframe as its boundary, use a jump. Otherwise use a fast exception that does not capture a stack trace. A stack trace is not needed since we know the exception will be handled (*) (*) To be 100% sure, this needs capture checking.
  • 11. Implementation The implementation of break produces efficient code. If break appears in the same stackframe as its boundary, use a jump. Otherwise use a fast exception that does not capture a stack trace. A stack trace is not needed since we know the exception will be handled (*) (*) To be 100% sure, this needs capture checking.
  • 12. Stage 2: Error handling boundary/break can be used as the basis for flexible error handling. For instance: def firstColumn[T](xss: List[List[T]]): Option[List[T]] = optional: xss.map(_.headOption.?) Optionally, returns the first column of the matrix xss. Returns None if there is an empty row.
  • 13. Error handling implementation optional and ? on options can be implemented quite easily on top of boundary/break: object optional: inline def apply[T](inline body: Label[None.type] ?=> T) : Option[T] = boundary(Some(body)) extension [T](r: Option[T]) inline def ? (using label: Label[None.type]): T = r match case Some(x) => x case None => break(None) Analogous implementations are possible for other result types such as Either or a Rust-like Result. My ideal way of error handling would be based on Result + ?.
  • 14. Stage 3: Suspensions Question: What if we could store the stack segment between a break and its boundary and re-use it at some later time?
  • 15. Suspensions Question: What if we could store the stack segment between a break and its boundary and re-use it at some later time? This is the idea of delimited continuations.
  • 16. Suspensions Question: What if we could store the stack segment between a break and its boundary and re-use it at some later time? This is the idea of delimited continuations.
  • 17. Suspension API class Suspension[-T, +R]: def resume(arg: T): R = ??? def suspend[T, R](body: Suspension[T, R] => R)(using Label[R]): T Suspensions are quite powerful. They can express at the same time algebraic effects and monads.
  • 18. Generators Python-style generators are a simple example of algebraic effects. def example = Generator: produce(”We’ll give you all the numbers divisible by 3 or 2”) for i <- 1 to 1000 do if i % 3 == 0 then produce(s”$i is divisible by 3”) else if i % 2 == 0 then produce(s”$i is even”) Here, Generator is essentially a simplified Iterator trait Generator[T]: def nextOption: Option[T]
  • 19. Algebraic Effects Task: Build a generate implementation of Generator, so that one can compute the leafs of a Tree like this: enum Tree[T]: case Leaf(x: T) case Inner(xs: List[Tree[T]]) def leafs[T](t: Tree[T]): Generator[T] = generate: // effect scope def recur(t: Tree[T]): Unit = t match case Tree.Leaf(x) => produce(x) // effect case Tree.Inner(xs) => xs.foreach(recur) recur(t)
  • 20. Generator Implementation trait Produce[-T]: def produce(x: T): Unit def generate[T](body: Produce[T] ?=> Unit) = new Generator[T]: def nextOption: Option[T] = step() var step: () => Option[T] =
  • 21. The Step Function trait Produce[-T]: // effect type def produce(x: T): Unit def generate[T](body: Produce[T] ?=> Unit) = new Generator[T]: def nextOption: Option[T] = step() var step: () => Option[T] = () => boundary: given Produce[T] with // handler def produce(x: T): Unit = suspend[Unit, Option[T]]: k => step = () => k.resume(()) Some(x) body None
  • 22. Summary: Algebraic Effects Effects are methods of effect traits Handlers are implementations of effect traits They are passed as implicit parameters. They can abort part of a computation via break They can also suspend part of a computation as a continuation and resume it later.
  • 23. Implementing Suspensions There are several possibilities: Directly in the runtime, as shown in the designs On top of fibers (requires some compromises) By bytecode rewriting (e.g. Quasar, javactrl) By source rewriting
  • 24. Suspensions and Monads: Wadler (1993): Continuations can be expressed as a monad. “Haskell is the essence of ML” Filinski (1994): Every monad can be expressed in direct style using just delimited continuations. “ML is the essence of Haskell” My take: designs based on continuations are simpler to compose than monads.
  • 25. Suspensions and Monads: Wadler (1993): Continuations can be expressed as a monad. “Haskell is the essence of ML” Filinski (1994): Every monad can be expressed in direct style using just delimited continuations. “ML is the essence of Haskell” My take: designs based on continuations are simpler to compose than monads.
  • 26. Direct-Style Futures With suspend(*), we can implement lightweight and universal await construct that can be called anywhere. This can express simple, direct-style futures. val sum = Future: val f1 = Future(c1.read) val f2 = Future(c2.read) f1.value + f2.value Structured concurrency: Local futures f1 and f2 complete before sum completes. This might mean that one of them is cancelled if the other returns with a failure. (*) Loom-like fibers would work as well.
  • 27. Compare with Status Quo val sum = val f1 = Future(c1.read) val f2 = Future(c2.read) for x <- f1 y <- f2 yield x + y Composition of futures is monadic but creation isn’t, which is a bit awkward.
  • 28. A Strawman lampepfl/async is an early stage prototype of a modern, low-level concurrency library in direct style. Main elements Futures: the primary active elements Channels: the primary passive elements Async Sources Futures and Channels both implement a new fundamental abstraction: an asynchronous source. Async Contexts An async context is a capability that allows a computation to suspend while waiting for the result of an async source. Link: github.com/lampepfl/async
  • 29. Futures The Future trait is defined as follows: trait Future[+T] extends Async.Source[Try[T]], Cancellable: def result(using Async): Try[T] def value(using Async): T = result.get The result method can be defined like this: def result(using async: Async): T = async.await(this) async is a capability that allows to suspend in an await method.
  • 30. Futures The Future trait is defined as follows: trait Future[+T] extends Async.Source[Try[T]], Cancellable: def result(using Async): Try[T] def value(using Async): T = result.get The result method can be defined like this: def result(using async: Async): T = async.await(this) async is a capability that allows to suspend in an await method.
  • 31. Async The Async trait is defined as follows: trait Async: def await[T](src: Async.Source[T]): T def scheduler: ExecutionContext def group: CancellationGroup def withGroup(group: CancellationGroup): Async await gets the (first) element of an Async.Source. It suspends if necessary.
  • 32. Async.Source Futures are a particular kind of an async source. (Other implementations come from channels). Async sources are the primary means of communication between asynchronous computations They can be composed in interesting ways. For instance, map and filter are provided: extension [T](s: Source[T]) def map[U](f: T => U): Source[U] def filter(p: T => Boolean): Source[T]
  • 33. Async.Source Futures are a particular kind of an async source. (Other implementations come from channels). Async sources are the primary means of communication between asynchronous computations They can be composed in interesting ways. For instance, map and filter are provided: extension [T](s: Source[T]) def map[U](f: T => U): Source[U] def filter(p: T => Boolean): Source[T]
  • 34. Races A race passes on the first of several sources: def race[T](sources: Source[T]*): Source[T] Higher-level operation: def either[T1, T2](src1: Source[T1], src2: Source[T2]) : Source[Either[T, U]] = race( src1.map(Left(_)), src2.map(Right(_)) )
  • 35. Structured Concurrency It’s now easy to implement zip and alt on futures: extension [T](f1: Future[T]) def zip[U](f2: Future[U])(using Async): Future[(T, U)] = Future: await(either(f1, f2)) match case Left(Success(x1)) => (x1, f2.value) case Right(Success(x2)) => (f1.value, x2) case Left(Failure(ex)) => throw ex case Right(Failure(ex)) => throw ex
  • 36. Structured Concurrency It’s now easy to implement zip and alt on futures: extension [T](f1: Future[T]) def alt(f2: Future[T])(using Async): Future[T] = Future: await(either(f1, f2)) match case Left(Success(x1)) => x1 case Right(Success(x2)) => x2 case Left(_: Failure[?]) => f2.value case Right(_: Failure[?]) => f1.value
  • 37. Why Futures & Channels? Futures: The simplest way to get parallelism Define a computation Run it in parallel Await the result when needed Channels: The canonical way of communication between computations. Both are instances as asynchronous sources
  • 38. Why not Coroutines? Often, coroutines (in the sense of CSP or goroutines) are used instead of futures to work with channels. But: We need to be able to wait for a coroutine’s termination. We need to handle any exceptions in the coroutine on the outside Both are achieved by using a Future[Unit]. So no different abstractions are needed.
  • 39. Why an ErrorType Fixed to Try? Natural solution if the language supports exception But common complaint for current futures: Error type is fixed to be Exception. This makes it awkward to handle other errors. For instance, how would you implement this function? def acrobatics(xs: List[Future[Result[T, E]]]) : Future[Result[List[T], E]] =
  • 40. Why an ErrorType Fixed to Try? Natural solution if the language supports exception But common complaint for current futures: Error type is fixed to be Exception. This makes it awkward to handle other errors. For instance, how would you implement this function? def acrobatics(xs: List[Future[Result[T, E]]]) : Future[Result[List[T], E]] =
  • 41. Why an ErrorType Fixed to Try? Natural solution if the language supports exception But common complaint for current futures: Error type is fixed to be Exception. This makes it awkward to handle other errors. For instance, how would you implement this function? def acrobatics(xs: List[Future[Result[T, E]]]) : Future[Result[List[T], E]] =
  • 42. Why an ErrorType Fixed to Try? Natural solution if the language supports exception But common complaint for current futures: Error type is fixed to be Exception. This makes it awkward to handle other errors. New direct style abstractions don’t have that problem anymore! def acrobatics(xs: List[Future[Result[T, E]]]) : Future[Result[List[T], E]] = Future: Result: xs.map(_.value.?) Simple compositions, no traverse or lift is needed.
  • 43. Conclusion Direct style has lots to offer Suspensions can express every monad, but, provide more flexible composition. This gives completely new possibilities to express practical foundations for concurrency and async I/O. The future will be interesting… Thank You
  • 44. Conclusion Direct style has lots to offer Suspensions can express every monad, but, provide more flexible composition. This gives completely new possibilities to express practical foundations for concurrency and async I/O. The future will be interesting… Thank You