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

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
 
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
 
Apache Calcite Tutorial - BOSS 21
Apache Calcite Tutorial - BOSS 21Apache Calcite Tutorial - BOSS 21
Apache Calcite Tutorial - BOSS 21Stamatis Zampetakis
 
[FFE19] Build a Flink AI Ecosystem
[FFE19] Build a Flink AI Ecosystem[FFE19] Build a Flink AI Ecosystem
[FFE19] Build a Flink AI EcosystemJiangjie Qin
 
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
 
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
 
How the Postgres Query Optimizer Works
How the Postgres Query Optimizer WorksHow the Postgres Query Optimizer Works
How the Postgres Query Optimizer WorksEDB
 
non-strict functions, bottom and scala by-name parameters
non-strict functions, bottom and scala by-name parametersnon-strict functions, bottom and scala by-name parameters
non-strict functions, bottom and scala by-name parametersPhilip Schwarz
 
Refactoring Functional Type Classes
Refactoring Functional Type ClassesRefactoring Functional Type Classes
Refactoring Functional Type ClassesJohn De Goes
 
Scala Talk at FOSDEM 2009
Scala Talk at FOSDEM 2009Scala Talk at FOSDEM 2009
Scala Talk at FOSDEM 2009Martin Odersky
 
Streaming Data Lakes using Kafka Connect + Apache Hudi | Vinoth Chandar, Apac...
Streaming Data Lakes using Kafka Connect + Apache Hudi | Vinoth Chandar, Apac...Streaming Data Lakes using Kafka Connect + Apache Hudi | Vinoth Chandar, Apac...
Streaming Data Lakes using Kafka Connect + Apache Hudi | Vinoth Chandar, Apac...HostedbyConfluent
 
Analysing and Troubleshooting Performance Issues in SAP BusinessObjects BI Re...
Analysing and Troubleshooting Performance Issues in SAP BusinessObjects BI Re...Analysing and Troubleshooting Performance Issues in SAP BusinessObjects BI Re...
Analysing and Troubleshooting Performance Issues in SAP BusinessObjects BI Re...BI Brainz
 
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
 
Izumi 1.0: Your Next Scala Stack
Izumi 1.0: Your Next Scala StackIzumi 1.0: Your Next Scala Stack
Izumi 1.0: Your Next Scala Stack7mind
 
ksqlDB로 실시간 데이터 변환 및 스트림 처리
ksqlDB로 실시간 데이터 변환 및 스트림 처리ksqlDB로 실시간 데이터 변환 및 스트림 처리
ksqlDB로 실시간 데이터 변환 및 스트림 처리confluent
 
Parquet performance tuning: the missing guide
Parquet performance tuning: the missing guideParquet performance tuning: the missing guide
Parquet performance tuning: the missing guideRyan Blue
 
SQL for NoSQL and how Apache Calcite can help
SQL for NoSQL and how  Apache Calcite can helpSQL for NoSQL and how  Apache Calcite can help
SQL for NoSQL and how Apache Calcite can helpChristian Tzolov
 

What's hot (20)

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...
 
SPARQL Cheat Sheet
SPARQL Cheat SheetSPARQL Cheat Sheet
SPARQL Cheat Sheet
 
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
 
Apache Calcite Tutorial - BOSS 21
Apache Calcite Tutorial - BOSS 21Apache Calcite Tutorial - BOSS 21
Apache Calcite Tutorial - BOSS 21
 
[FFE19] Build a Flink AI Ecosystem
[FFE19] Build a Flink AI Ecosystem[FFE19] Build a Flink AI Ecosystem
[FFE19] Build a Flink AI Ecosystem
 
Zio in real world
Zio in real worldZio in real world
Zio in real world
 
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...
 
Boost your productivity with Scala tooling!
Boost your productivity  with Scala tooling!Boost your productivity  with Scala tooling!
Boost your productivity with Scala tooling!
 
How the Postgres Query Optimizer Works
How the Postgres Query Optimizer WorksHow the Postgres Query Optimizer Works
How the Postgres Query Optimizer Works
 
Scala cheatsheet
Scala cheatsheetScala cheatsheet
Scala cheatsheet
 
non-strict functions, bottom and scala by-name parameters
non-strict functions, bottom and scala by-name parametersnon-strict functions, bottom and scala by-name parameters
non-strict functions, bottom and scala by-name parameters
 
Refactoring Functional Type Classes
Refactoring Functional Type ClassesRefactoring Functional Type Classes
Refactoring Functional Type Classes
 
Scala Talk at FOSDEM 2009
Scala Talk at FOSDEM 2009Scala Talk at FOSDEM 2009
Scala Talk at FOSDEM 2009
 
Streaming Data Lakes using Kafka Connect + Apache Hudi | Vinoth Chandar, Apac...
Streaming Data Lakes using Kafka Connect + Apache Hudi | Vinoth Chandar, Apac...Streaming Data Lakes using Kafka Connect + Apache Hudi | Vinoth Chandar, Apac...
Streaming Data Lakes using Kafka Connect + Apache Hudi | Vinoth Chandar, Apac...
 
Analysing and Troubleshooting Performance Issues in SAP BusinessObjects BI Re...
Analysing and Troubleshooting Performance Issues in SAP BusinessObjects BI Re...Analysing and Troubleshooting Performance Issues in SAP BusinessObjects BI Re...
Analysing and Troubleshooting Performance Issues in SAP BusinessObjects BI Re...
 
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
 
Izumi 1.0: Your Next Scala Stack
Izumi 1.0: Your Next Scala StackIzumi 1.0: Your Next Scala Stack
Izumi 1.0: Your Next Scala Stack
 
ksqlDB로 실시간 데이터 변환 및 스트림 처리
ksqlDB로 실시간 데이터 변환 및 스트림 처리ksqlDB로 실시간 데이터 변환 및 스트림 처리
ksqlDB로 실시간 데이터 변환 및 스트림 처리
 
Parquet performance tuning: the missing guide
Parquet performance tuning: the missing guideParquet performance tuning: the missing guide
Parquet performance tuning: the missing guide
 
SQL for NoSQL and how Apache Calcite can help
SQL for NoSQL and how  Apache Calcite can helpSQL for NoSQL and how  Apache Calcite can help
SQL for NoSQL and how Apache Calcite can help
 

Similar to scalar.pdf

Евгений Бурмако «scala.reflect»
Евгений Бурмако «scala.reflect»Евгений Бурмако «scala.reflect»
Евгений Бурмако «scala.reflect»e-Legion
 
scala.reflect, Eugene Burmako
scala.reflect, Eugene Burmakoscala.reflect, Eugene Burmako
scala.reflect, Eugene BurmakoVasil Remeniuk
 
A Survey of Concurrency Constructs
A Survey of Concurrency ConstructsA Survey of Concurrency Constructs
A Survey of Concurrency ConstructsTed Leung
 
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
 
Sequence and Traverse - Part 2
Sequence and Traverse - Part 2Sequence and Traverse - Part 2
Sequence and Traverse - Part 2Philip Schwarz
 

Similar to scalar.pdf (20)

Евгений Бурмако «scala.reflect»
Евгений Бурмако «scala.reflect»Евгений Бурмако «scala.reflect»
Евгений Бурмако «scala.reflect»
 
scala.reflect, Eugene Burmako
scala.reflect, Eugene Burmakoscala.reflect, Eugene Burmako
scala.reflect, Eugene Burmako
 
A Survey of Concurrency Constructs
A Survey of Concurrency ConstructsA Survey of Concurrency Constructs
A Survey of Concurrency Constructs
 
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
 
Sequence and Traverse - Part 2
Sequence and Traverse - Part 2Sequence and Traverse - Part 2
Sequence and Traverse - Part 2
 

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 (16)

Preparing for Scala 3
Preparing for Scala 3Preparing for Scala 3
Preparing for Scala 3
 
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

Crafting the Perfect Measurement Sheet with PLM Integration
Crafting the Perfect Measurement Sheet with PLM IntegrationCrafting the Perfect Measurement Sheet with PLM Integration
Crafting the Perfect Measurement Sheet with PLM IntegrationWave PLM
 
IT Software Development Resume, Vaibhav jha 2024
IT Software Development Resume, Vaibhav jha 2024IT Software Development Resume, Vaibhav jha 2024
IT Software Development Resume, Vaibhav jha 2024vaibhav130304
 
how-to-download-files-safely-from-the-internet.pdf
how-to-download-files-safely-from-the-internet.pdfhow-to-download-files-safely-from-the-internet.pdf
how-to-download-files-safely-from-the-internet.pdfMehmet Akar
 
SQL Injection Introduction and Prevention
SQL Injection Introduction and PreventionSQL Injection Introduction and Prevention
SQL Injection Introduction and PreventionMohammed Fazuluddin
 
JustNaik Solution Deck (stage bus sector)
JustNaik Solution Deck (stage bus sector)JustNaik Solution Deck (stage bus sector)
JustNaik Solution Deck (stage bus sector)Max Lee
 
Salesforce Introduced Zero Copy Partner Network to Simplify the Process of In...
Salesforce Introduced Zero Copy Partner Network to Simplify the Process of In...Salesforce Introduced Zero Copy Partner Network to Simplify the Process of In...
Salesforce Introduced Zero Copy Partner Network to Simplify the Process of In...CloudMetic
 
Sourcing Success - How to Find a Clothing Manufacturer
Sourcing Success - How to Find a Clothing ManufacturerSourcing Success - How to Find a Clothing Manufacturer
Sourcing Success - How to Find a Clothing ManufacturerWave PLM
 
Workforce Efficiency with Employee Time Tracking Software.pdf
Workforce Efficiency with Employee Time Tracking Software.pdfWorkforce Efficiency with Employee Time Tracking Software.pdf
Workforce Efficiency with Employee Time Tracking Software.pdfDeskTrack
 
Implementing KPIs and Right Metrics for Agile Delivery Teams.pdf
Implementing KPIs and Right Metrics for Agile Delivery Teams.pdfImplementing KPIs and Right Metrics for Agile Delivery Teams.pdf
Implementing KPIs and Right Metrics for Agile Delivery Teams.pdfVictor Lopez
 
INGKA DIGITAL: Linked Metadata by Design
INGKA DIGITAL: Linked Metadata by DesignINGKA DIGITAL: Linked Metadata by Design
INGKA DIGITAL: Linked Metadata by DesignNeo4j
 
Weeding your micro service landscape.pdf
Weeding your micro service landscape.pdfWeeding your micro service landscape.pdf
Weeding your micro service landscape.pdftimtebeek1
 
OpenChain @ LF Japan Executive Briefing - May 2024
OpenChain @ LF Japan Executive Briefing - May 2024OpenChain @ LF Japan Executive Briefing - May 2024
OpenChain @ LF Japan Executive Briefing - May 2024Shane Coughlan
 
How to install and activate eGrabber JobGrabber
How to install and activate eGrabber JobGrabberHow to install and activate eGrabber JobGrabber
How to install and activate eGrabber JobGrabbereGrabber
 
Reinforcement Learning – a Rewards Based Approach to Machine Learning - Marko...
Reinforcement Learning – a Rewards Based Approach to Machine Learning - Marko...Reinforcement Learning – a Rewards Based Approach to Machine Learning - Marko...
Reinforcement Learning – a Rewards Based Approach to Machine Learning - Marko...Marko Lohert
 
Anypoint Code Builder - Munich MuleSoft Meetup - 16th May 2024
Anypoint Code Builder - Munich MuleSoft Meetup - 16th May 2024Anypoint Code Builder - Munich MuleSoft Meetup - 16th May 2024
Anypoint Code Builder - Munich MuleSoft Meetup - 16th May 2024MulesoftMunichMeetup
 
The Impact of PLM Software on Fashion Production
The Impact of PLM Software on Fashion ProductionThe Impact of PLM Software on Fashion Production
The Impact of PLM Software on Fashion ProductionWave PLM
 
Secure Software Ecosystem Teqnation 2024
Secure Software Ecosystem Teqnation 2024Secure Software Ecosystem Teqnation 2024
Secure Software Ecosystem Teqnation 2024Soroosh Khodami
 
Community is Just as Important as Code by Andrea Goulet
Community is Just as Important as Code by Andrea GouletCommunity is Just as Important as Code by Andrea Goulet
Community is Just as Important as Code by Andrea GouletAndrea Goulet
 
COMPUTER AND ITS COMPONENTS PPT.by naitik sharma Class 9th A mittal internati...
COMPUTER AND ITS COMPONENTS PPT.by naitik sharma Class 9th A mittal internati...COMPUTER AND ITS COMPONENTS PPT.by naitik sharma Class 9th A mittal internati...
COMPUTER AND ITS COMPONENTS PPT.by naitik sharma Class 9th A mittal internati...naitiksharma1124
 

Recently uploaded (20)

Crafting the Perfect Measurement Sheet with PLM Integration
Crafting the Perfect Measurement Sheet with PLM IntegrationCrafting the Perfect Measurement Sheet with PLM Integration
Crafting the Perfect Measurement Sheet with PLM Integration
 
IT Software Development Resume, Vaibhav jha 2024
IT Software Development Resume, Vaibhav jha 2024IT Software Development Resume, Vaibhav jha 2024
IT Software Development Resume, Vaibhav jha 2024
 
how-to-download-files-safely-from-the-internet.pdf
how-to-download-files-safely-from-the-internet.pdfhow-to-download-files-safely-from-the-internet.pdf
how-to-download-files-safely-from-the-internet.pdf
 
SQL Injection Introduction and Prevention
SQL Injection Introduction and PreventionSQL Injection Introduction and Prevention
SQL Injection Introduction and Prevention
 
JustNaik Solution Deck (stage bus sector)
JustNaik Solution Deck (stage bus sector)JustNaik Solution Deck (stage bus sector)
JustNaik Solution Deck (stage bus sector)
 
Salesforce Introduced Zero Copy Partner Network to Simplify the Process of In...
Salesforce Introduced Zero Copy Partner Network to Simplify the Process of In...Salesforce Introduced Zero Copy Partner Network to Simplify the Process of In...
Salesforce Introduced Zero Copy Partner Network to Simplify the Process of In...
 
Sourcing Success - How to Find a Clothing Manufacturer
Sourcing Success - How to Find a Clothing ManufacturerSourcing Success - How to Find a Clothing Manufacturer
Sourcing Success - How to Find a Clothing Manufacturer
 
Workforce Efficiency with Employee Time Tracking Software.pdf
Workforce Efficiency with Employee Time Tracking Software.pdfWorkforce Efficiency with Employee Time Tracking Software.pdf
Workforce Efficiency with Employee Time Tracking Software.pdf
 
Implementing KPIs and Right Metrics for Agile Delivery Teams.pdf
Implementing KPIs and Right Metrics for Agile Delivery Teams.pdfImplementing KPIs and Right Metrics for Agile Delivery Teams.pdf
Implementing KPIs and Right Metrics for Agile Delivery Teams.pdf
 
INGKA DIGITAL: Linked Metadata by Design
INGKA DIGITAL: Linked Metadata by DesignINGKA DIGITAL: Linked Metadata by Design
INGKA DIGITAL: Linked Metadata by Design
 
Weeding your micro service landscape.pdf
Weeding your micro service landscape.pdfWeeding your micro service landscape.pdf
Weeding your micro service landscape.pdf
 
OpenChain @ LF Japan Executive Briefing - May 2024
OpenChain @ LF Japan Executive Briefing - May 2024OpenChain @ LF Japan Executive Briefing - May 2024
OpenChain @ LF Japan Executive Briefing - May 2024
 
How to install and activate eGrabber JobGrabber
How to install and activate eGrabber JobGrabberHow to install and activate eGrabber JobGrabber
How to install and activate eGrabber JobGrabber
 
Reinforcement Learning – a Rewards Based Approach to Machine Learning - Marko...
Reinforcement Learning – a Rewards Based Approach to Machine Learning - Marko...Reinforcement Learning – a Rewards Based Approach to Machine Learning - Marko...
Reinforcement Learning – a Rewards Based Approach to Machine Learning - Marko...
 
Anypoint Code Builder - Munich MuleSoft Meetup - 16th May 2024
Anypoint Code Builder - Munich MuleSoft Meetup - 16th May 2024Anypoint Code Builder - Munich MuleSoft Meetup - 16th May 2024
Anypoint Code Builder - Munich MuleSoft Meetup - 16th May 2024
 
AI Hackathon.pptx
AI                        Hackathon.pptxAI                        Hackathon.pptx
AI Hackathon.pptx
 
The Impact of PLM Software on Fashion Production
The Impact of PLM Software on Fashion ProductionThe Impact of PLM Software on Fashion Production
The Impact of PLM Software on Fashion Production
 
Secure Software Ecosystem Teqnation 2024
Secure Software Ecosystem Teqnation 2024Secure Software Ecosystem Teqnation 2024
Secure Software Ecosystem Teqnation 2024
 
Community is Just as Important as Code by Andrea Goulet
Community is Just as Important as Code by Andrea GouletCommunity is Just as Important as Code by Andrea Goulet
Community is Just as Important as Code by Andrea Goulet
 
COMPUTER AND ITS COMPONENTS PPT.by naitik sharma Class 9th A mittal internati...
COMPUTER AND ITS COMPONENTS PPT.by naitik sharma Class 9th A mittal internati...COMPUTER AND ITS COMPONENTS PPT.by naitik sharma Class 9th A mittal internati...
COMPUTER AND ITS COMPONENTS PPT.by naitik sharma Class 9th A mittal internati...
 

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