SlideShare une entreprise Scribd logo
1  sur  43
Scala - The Simple Parts
Martin Odersky
Typesafe and EPFL
10 Years of Scala
Grown Up?
Scala’s user community is pretty large for its age group.
And, despite it coming out of academia it is amazingly
successful in practice.
But Scala is also discussed more controversially than
usual for a language at its stage of adoption.
Why?
3
Controversies
Internal controversies: Different communities not agreeing
what programming in Scala should be.
External complaints:
“Scala is too academic”
“Scala has sold out to industry”
“Scala’s types are too hard”
“Scala’s types are not strict enough”
“Scala is everything and the kitchen sink”
Signs that we have not made clear enough what the
essence of programming in Scala is.
4
1-5
The Picture So Far
Agile, with lightweight syntax
Object-Oriented Functional
Safe and performant, with strong static tpying
= scalable
What is “Scalable”?
• 1st meaning: “Growable”
– can be molded into new languages
by adding libraries (domain
specific or general)
See: “Growing a language”
(Guy Steele, 1998)
• 2nd meaning: “Enabling Growth”
– can be used for small as well as
large systems
– allows for smooth growth from
small to large.
6
A Growable Language
• Flexible Syntax
• Flexible Types
• User-definable operators
• Higher-order functions
• Implicits
...
• Make it relatively easy to build new DSLs on top of Scala
• And where this fails, we can always use the macro
system (even though so far it’s labeled experimental)
7
A Growable Language
8
SBT
Chisel
Spark
Spray
Dispatch
Akka
ScalaTest
Squeryl
Specs
shapeless
Scalaz
Slick
Growable = Good?
In fact, it’s a double edged sword.
• DSLs can fraction the user community (“The Lisp curse”)
• Besides, no language is liked by everyone, no matter
whether its a DSL or general purpose.
• Host languages get the blame for the DSLs they embed.
Growable is great for experimentation.
But it demands conformity and discipline for large scale
production use.
9
A Language For Growth
• Can start with a one-liner.
• Can experiment quickly.
• Can grow without fearing to fall off the cliff.
• Scala deployments now go into the millions of lines of
code.
– Language works for very large programs
– Tools are challenged (build times!) but are catching up.
“A large system is one where you do not know that some of
its components even exist”
10
What Enables Growth?
• Unique combination of Object/Oriented and Functional
• Large systems rely on both.
Object-Oriented Functional
• Unfortunately, there’s no established term for this
object/functional?
11
12
Would prefer it like this
OOPFP
But unfortunately it’s often more like this
OOP
FP

And that’s where we are 
How many OOP
people see FP
How many FP
people see OOP
1-14
A Modular Language!
Large Systems
Object-Oriented Functional
Small Scripts
= modular
Modular Programming
• Systems should be composed from modules.
• Modules should be simple parts that can be combined in
many ways to give interesting results.
(Simple: encapsulates one functionality)
But that’s old hat!
– Should we go back to Modula-2?
– Modula-2 was limited by the Von-Neumann Bottleneck (see John
Backus’ Turing award lecture).
– Today’s systems need richer types.
15
FP is Essential for Modular Programming
Read:
“Why Functional Programming Matters”
(John Hughes, 1985).
Paraphrasing:
“Functional Programming is good because it leads to
modules that can be combined freely.”
16
Functions and Modules
• Functional does not always imply Modular.
• Non-modular concepts in functional languages:
– Haskell’s type classes
– OCaml’s records and variants
– Dynamic typing
17
Objects and Modules
• Object-oriented languages are in a sense the successors
of classical modular languages.
• But Object-Oriented does not always imply Modular
either.
• Non-modular concepts in OOP languages:
– Smalltalk’s virtual image.
– Monkey-patching
– Mutable state makes transformations hard.
– Weak composition facilities require external DI frameworks.
– Weak decomposition facilities encourage mixing applications
with domain logic.
18
Scala – The Simple Parts
Before discussing library modules, let’s start with the
simple parts in the language itself.
Here are seven simple building blocks that can be
combined in many ways.
Together, they cover much of Scala’s programming in the
small.
Warning: This is pretty boring.
19
#1 Expressions
Everything is an expression
if (age >= 18) "grownup" else "minor"
val result = try {
val people = file.iterator
people.map(_.age).max
} catch {
case ex: IOException =>
-1
}
20
#2: Scopes
• Everything can be nested.
• Static scoping discipline.
• Two name spaces: Terms and Types.
• Same rules for each.
21
#3: Patterns and Case Classes
trait Expr
case class Number(n: Int) extends Expr
case class Plus(l: Expr, r: Expr) extends Expr
def eval(e: Expr): Int = e match {
case Number(n) => n
case Plus(l, r) => eval(l) + eval(r)
}
Simple & flexible, even if a bit verbose.
22
#4: Recursion
• Recursion is almost always better than a loop.
• Simple fallback: Tail-recursive functions
• Guaranteed to be efficient
@tailrec
def loop(xs: List[T], ys: List[U]): Boolean =
if (xs.isEmpty) ys.isEmpty
else ys.nonEmpty && loop(xs.tail, ys.tail)
23
#5: Function Values
• Functions are values
• Can be named or anonymous
• Scope rules are correct.
def isMinor(p: Person) = p.age < 18
val (minors, adults) = people.partition(isMinor)
val infants = minors.filter(_.age <= 3)
• (this one is pretty standard by now)
24
#6 Collections
• Extensible set of collection types
• Uniform operations
• Transforms instead of CRUD
• Very simple to use
25
Collection Objection
“The type of map is ugly / a lie”
26
Collection Objection
“The type of map is ugly / a lie”
27
Why CanBuildFrom?
• Why not define it like this?
class Functor[F[_], T] {
def map[U](f: T => U): F[U]
}
• Does not work for arrays, since we need a class-tag to
build a new array.
• More generally, does not work in any case where we
need some additional information to build a new
collection.
• This is precisely what’s achieved by CanBuildFrom.
28
Collection Objection
“Set is not a Functor”
WAT? Let’s check Wikipedia:
(in fact, Set is in some sense the canonical functor!)
29
Who’s right, Mathematics or Haskell?
Crucial difference:
– In mathematics, a type comes with an equality
– In programming, a single conceptual value might have more than
one representation, so equality has to be given explicitly.
– If we construct a set, we need an equality operation to avoid
duplicates.
– In Haskell this is given by a typeclass Eq.
– But the naive functor type
def map[U](f: T => U): F[U]
does not accommodate this type class.
So the “obvious” type signature for `map` is inadequate.
Again, CanBuildFrom fixes this.
30
#7 Vars
• Are vars not anti-modular?
• Indeed, global state often leads to hidden dependencies.
• But used-wisely, mutable state can cut down on
annoying boilerplate and increase clarity.
31
Comparing Compilers
scalac: Mixed imperative/functional
– Started from a Java codebase.
– Mutable state is over-used.
– Now we know better.
dotc: Mostly functional architecture. State is used for
– caching lazy vals, memoized functions,
interned names, LRU caches.
– persisting once a value is stable, store it in an object.
– copy on write avoid copying untpd.Tree to tpd.Tree.
– fresh values fresh names, unique ids
– typer state current constraint & current diagnostics
(versioned, explorable).
32
Why Not Use a Monad?
The fundamentalist functional approach would mandate that
typer state is represented as a monad.
Instead of now:
def typed(tree: untpd.Tree, expected: Type): tpd.Tree
def isSubType(tp1: Type, tp2: Type): Boolean
we’d write:
def typed(tree: untpd.Tree, expected: Type):
TyperState[tps.Tree]
def isSubType(tp1: Type, tp2: Type):
TyperState[Boolean]
33
Why Not Use a Monad?
Instead of now:
if (isSubType(t1, t2) && isSubType(t2, t3)) result
we’d write:
for {
c1 <- isSubType(t1, t2)
c2 <- isSubType(t2, t3)
if c1 && c2
} yield result
Why would this be better?
34
Scala’s Modular Roots
Modula-2 First language I programmed intensively
First language for which I wrote a compiler.
Modula-3 Introduced universal subtyping
Haskell Type classes  Implicits
SML modules
Object ≅ Structure
Class ≅ Functor
Trait ≅ Signature
Abstract Type ≅ Abstract Type
Refinement ≅ Sharing Constraint
35
Features Supporting Modular Programming
1. Rich types with functional semantics.
– gives us the domains of discourse
2. Static typing.
– Gives us the means to guarantee encapsulation
– Read
“On the Criteria for Decomposing Systems into Modules”
(David Parnas, 1972)
3. Objects
4. Classes and Traits
36
#5 Abstract Types
trait Food
class Grass extends Food
trait Animal {
type Diet <: Food
def eat(food: Diet)
}
class Cow extends Animal with Food {
type Diet = Grass
def eat(food: Grass) = ???
}
class Lion extends Animal {
type Diet = Cow
def eat(food: Cow) = ???
}
37
#6 Parameterized Types
class List[+T]
class Set[T]
class Function1[-T, +R]
List[Number]
Set[String]
Function1[String, Int]
Variance expressed by +/- annotations
A good way to explain variance is by mapping to abstract
types.
38
Modelling Parameterized Types
class Set[T] { ... }  class Set { type $T }
Set[String]  Set { type $T = String }
class List[+T] { ... }  class List { type $T }List[Number]
 List { type $T <: Number }
Parameters  Abstract members
Arguments  Refinements
#7 Implicit Parameters
Implicit parameters are a simple concept
But they are surprisingly versatile.
Can represent a typeclass:
def min(x: A, b: A)(implicit cmp: Ordering[A]): A
40
#7 Implicit Parameters
Can represent a context
def typed(tree: untpd.Tree, expected:
Type)(implicit ctx: Context): Type
def compile(cmdLine: String)
(implicit defaultOptions: List[String]): Unit
Can represent a capability:
def accessProfile(id: CustomerId)
(implicit admin: AdminRights): Info
41
Simple Parts Summary
Language
Expressions
Scopes and Nesting
Case Classes and Patterns
Recursion
Function Values
Collections
Vars
A fairly modest and boring set of parts that can be
combined in many ways.
(Boring is good!)
42
Library
Static Types
Objects
Classes
Traits
Abstract Types
Type Parameters
Implicit Parameters
Thank You
Follow us on twitter:
@typesafe

Contenu connexe

Tendances

What To Leave Implicit
What To Leave ImplicitWhat To Leave Implicit
What To Leave ImplicitMartin 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
 
Scala Talk at FOSDEM 2009
Scala Talk at FOSDEM 2009Scala Talk at FOSDEM 2009
Scala Talk at FOSDEM 2009Martin Odersky
 
Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to ScalaRahul Jain
 
What To Leave Implicit
What To Leave ImplicitWhat To Leave Implicit
What To Leave ImplicitMartin Odersky
 
Introduction to Functional Programming with Scala
Introduction to Functional Programming with ScalaIntroduction to Functional Programming with Scala
Introduction to Functional Programming with Scalapramode_ce
 
Scala : language of the future
Scala : language of the futureScala : language of the future
Scala : language of the futureAnsviaLab
 
Haskell vs. F# vs. Scala
Haskell vs. F# vs. ScalaHaskell vs. F# vs. Scala
Haskell vs. F# vs. Scalapt114
 
A Brief Intro to Scala
A Brief Intro to ScalaA Brief Intro to Scala
A Brief Intro to ScalaTim Underwood
 
Advanced Functional Programming in Scala
Advanced Functional Programming in ScalaAdvanced Functional Programming in Scala
Advanced Functional Programming in ScalaPatrick Nicolas
 
The Evolution of Scala / Scala進化論
The Evolution of Scala / Scala進化論The Evolution of Scala / Scala進化論
The Evolution of Scala / Scala進化論scalaconfjp
 
Why Scala for Web 2.0?
Why Scala for Web 2.0?Why Scala for Web 2.0?
Why Scala for Web 2.0?Alex Payne
 
Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to ScalaSaleem Ansari
 

Tendances (19)

Quick introduction to scala
Quick introduction to scalaQuick introduction to scala
Quick introduction to scala
 
What To Leave Implicit
What To Leave ImplicitWhat To Leave Implicit
What To Leave Implicit
 
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
 
Scala Talk at FOSDEM 2009
Scala Talk at FOSDEM 2009Scala Talk at FOSDEM 2009
Scala Talk at FOSDEM 2009
 
Preparing for Scala 3
Preparing for Scala 3Preparing for Scala 3
Preparing for Scala 3
 
Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to Scala
 
What To Leave Implicit
What To Leave ImplicitWhat To Leave Implicit
What To Leave Implicit
 
Introduction to Functional Programming with Scala
Introduction to Functional Programming with ScalaIntroduction to Functional Programming with Scala
Introduction to Functional Programming with Scala
 
Scala Intro
Scala IntroScala Intro
Scala Intro
 
Simplicitly
SimplicitlySimplicitly
Simplicitly
 
Scala : language of the future
Scala : language of the futureScala : language of the future
Scala : language of the future
 
Haskell vs. F# vs. Scala
Haskell vs. F# vs. ScalaHaskell vs. F# vs. Scala
Haskell vs. F# vs. Scala
 
A Brief Intro to Scala
A Brief Intro to ScalaA Brief Intro to Scala
A Brief Intro to Scala
 
Advanced Functional Programming in Scala
Advanced Functional Programming in ScalaAdvanced Functional Programming in Scala
Advanced Functional Programming in Scala
 
The Evolution of Scala / Scala進化論
The Evolution of Scala / Scala進化論The Evolution of Scala / Scala進化論
The Evolution of Scala / Scala進化論
 
Scala Days NYC 2016
Scala Days NYC 2016Scala Days NYC 2016
Scala Days NYC 2016
 
Why Scala for Web 2.0?
Why Scala for Web 2.0?Why Scala for Web 2.0?
Why Scala for Web 2.0?
 
Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to Scala
 

Similaire à Scala - The Simple Building Blocks of Modular Programming

scalaliftoff2009.pdf
scalaliftoff2009.pdfscalaliftoff2009.pdf
scalaliftoff2009.pdfHiroshi Ono
 
scalaliftoff2009.pdf
scalaliftoff2009.pdfscalaliftoff2009.pdf
scalaliftoff2009.pdfHiroshi Ono
 
scalaliftoff2009.pdf
scalaliftoff2009.pdfscalaliftoff2009.pdf
scalaliftoff2009.pdfHiroshi Ono
 
scalaliftoff2009.pdf
scalaliftoff2009.pdfscalaliftoff2009.pdf
scalaliftoff2009.pdfHiroshi Ono
 
Scala final ppt vinay
Scala final ppt vinayScala final ppt vinay
Scala final ppt vinayViplav Jain
 
ScalaDays 2013 Keynote Speech by Martin Odersky
ScalaDays 2013 Keynote Speech by Martin OderskyScalaDays 2013 Keynote Speech by Martin Odersky
ScalaDays 2013 Keynote Speech by Martin OderskyTypesafe
 
Introducing Scala to your Ruby/Java Shop : My experiences at IGN
Introducing Scala to your Ruby/Java Shop : My experiences at IGNIntroducing Scala to your Ruby/Java Shop : My experiences at IGN
Introducing Scala to your Ruby/Java Shop : My experiences at IGNManish Pandit
 
Principled io in_scala_2019_distribution
Principled io in_scala_2019_distributionPrincipled io in_scala_2019_distribution
Principled io in_scala_2019_distributionRaymond Tay
 
About Functional Programming
About Functional ProgrammingAbout Functional Programming
About Functional ProgrammingAapo Kyrölä
 
Martin Odersky: What's next for Scala
Martin Odersky: What's next for ScalaMartin Odersky: What's next for Scala
Martin Odersky: What's next for ScalaMarakana Inc.
 
Are High Level Programming Languages for Multicore and Safety Critical Conver...
Are High Level Programming Languages for Multicore and Safety Critical Conver...Are High Level Programming Languages for Multicore and Safety Critical Conver...
Are High Level Programming Languages for Multicore and Safety Critical Conver...InfinIT - Innovationsnetværket for it
 
Programming in Scala - Lecture One
Programming in Scala - Lecture OneProgramming in Scala - Lecture One
Programming in Scala - Lecture OneAngelo Corsaro
 
Spark - The Ultimate Scala Collections by Martin Odersky
Spark - The Ultimate Scala Collections by Martin OderskySpark - The Ultimate Scala Collections by Martin Odersky
Spark - The Ultimate Scala Collections by Martin OderskySpark Summit
 
Scala for n00bs by a n00b.
Scala for n00bs by a n00b.Scala for n00bs by a n00b.
Scala for n00bs by a n00b.brandongulla
 
Introduction to scala for a c programmer
Introduction to scala for a c programmerIntroduction to scala for a c programmer
Introduction to scala for a c programmerGirish Kumar A L
 
Scala - from "Hello, World" to "Heroku Scale"
Scala - from "Hello, World" to "Heroku Scale"Scala - from "Hello, World" to "Heroku Scale"
Scala - from "Hello, World" to "Heroku Scale"Salesforce Developers
 

Similaire à Scala - The Simple Building Blocks of Modular Programming (20)

scalaliftoff2009.pdf
scalaliftoff2009.pdfscalaliftoff2009.pdf
scalaliftoff2009.pdf
 
scalaliftoff2009.pdf
scalaliftoff2009.pdfscalaliftoff2009.pdf
scalaliftoff2009.pdf
 
scalaliftoff2009.pdf
scalaliftoff2009.pdfscalaliftoff2009.pdf
scalaliftoff2009.pdf
 
scalaliftoff2009.pdf
scalaliftoff2009.pdfscalaliftoff2009.pdf
scalaliftoff2009.pdf
 
Scala final ppt vinay
Scala final ppt vinayScala final ppt vinay
Scala final ppt vinay
 
ScalaDays 2013 Keynote Speech by Martin Odersky
ScalaDays 2013 Keynote Speech by Martin OderskyScalaDays 2013 Keynote Speech by Martin Odersky
ScalaDays 2013 Keynote Speech by Martin Odersky
 
Introducing Scala to your Ruby/Java Shop : My experiences at IGN
Introducing Scala to your Ruby/Java Shop : My experiences at IGNIntroducing Scala to your Ruby/Java Shop : My experiences at IGN
Introducing Scala to your Ruby/Java Shop : My experiences at IGN
 
Principled io in_scala_2019_distribution
Principled io in_scala_2019_distributionPrincipled io in_scala_2019_distribution
Principled io in_scala_2019_distribution
 
About Functional Programming
About Functional ProgrammingAbout Functional Programming
About Functional Programming
 
Martin Odersky: What's next for Scala
Martin Odersky: What's next for ScalaMartin Odersky: What's next for Scala
Martin Odersky: What's next for Scala
 
Are High Level Programming Languages for Multicore and Safety Critical Conver...
Are High Level Programming Languages for Multicore and Safety Critical Conver...Are High Level Programming Languages for Multicore and Safety Critical Conver...
Are High Level Programming Languages for Multicore and Safety Critical Conver...
 
Metamorphic Domain-Specific Languages
Metamorphic Domain-Specific LanguagesMetamorphic Domain-Specific Languages
Metamorphic Domain-Specific Languages
 
Scala-Ls1
Scala-Ls1Scala-Ls1
Scala-Ls1
 
Programming in Scala - Lecture One
Programming in Scala - Lecture OneProgramming in Scala - Lecture One
Programming in Scala - Lecture One
 
Spark - The Ultimate Scala Collections by Martin Odersky
Spark - The Ultimate Scala Collections by Martin OderskySpark - The Ultimate Scala Collections by Martin Odersky
Spark - The Ultimate Scala Collections by Martin Odersky
 
Scala for n00bs by a n00b.
Scala for n00bs by a n00b.Scala for n00bs by a n00b.
Scala for n00bs by a n00b.
 
Introduction to scala for a c programmer
Introduction to scala for a c programmerIntroduction to scala for a c programmer
Introduction to scala for a c programmer
 
General oop concept
General oop conceptGeneral oop concept
General oop concept
 
Scala - from "Hello, World" to "Heroku Scale"
Scala - from "Hello, World" to "Heroku Scale"Scala - from "Hello, World" to "Heroku Scale"
Scala - from "Hello, World" to "Heroku Scale"
 
scala-intro
scala-introscala-intro
scala-intro
 

Dernier

A healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfA healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfMarharyta Nedzelska
 
Enhancing Supply Chain Visibility with Cargo Cloud Solutions.pdf
Enhancing Supply Chain Visibility with Cargo Cloud Solutions.pdfEnhancing Supply Chain Visibility with Cargo Cloud Solutions.pdf
Enhancing Supply Chain Visibility with Cargo Cloud Solutions.pdfRTS corp
 
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Angel Borroy López
 
Large Language Models for Test Case Evolution and Repair
Large Language Models for Test Case Evolution and RepairLarge Language Models for Test Case Evolution and Repair
Large Language Models for Test Case Evolution and RepairLionel Briand
 
SAM Training Session - How to use EXCEL ?
SAM Training Session - How to use EXCEL ?SAM Training Session - How to use EXCEL ?
SAM Training Session - How to use EXCEL ?Alexandre Beguel
 
The Role of IoT and Sensor Technology in Cargo Cloud Solutions.pptx
The Role of IoT and Sensor Technology in Cargo Cloud Solutions.pptxThe Role of IoT and Sensor Technology in Cargo Cloud Solutions.pptx
The Role of IoT and Sensor Technology in Cargo Cloud Solutions.pptxRTS corp
 
2024 DevNexus Patterns for Resiliency: Shuffle shards
2024 DevNexus Patterns for Resiliency: Shuffle shards2024 DevNexus Patterns for Resiliency: Shuffle shards
2024 DevNexus Patterns for Resiliency: Shuffle shardsChristopher Curtin
 
Keeping your build tool updated in a multi repository world
Keeping your build tool updated in a multi repository worldKeeping your build tool updated in a multi repository world
Keeping your build tool updated in a multi repository worldRoberto Pérez Alcolea
 
Understanding Flamingo - DeepMind's VLM Architecture
Understanding Flamingo - DeepMind's VLM ArchitectureUnderstanding Flamingo - DeepMind's VLM Architecture
Understanding Flamingo - DeepMind's VLM Architecturerahul_net
 
VK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web DevelopmentVK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web Developmentvyaparkranti
 
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...confluent
 
Precise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalPrecise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalLionel Briand
 
Post Quantum Cryptography – The Impact on Identity
Post Quantum Cryptography – The Impact on IdentityPost Quantum Cryptography – The Impact on Identity
Post Quantum Cryptography – The Impact on Identityteam-WIBU
 
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...OnePlan Solutions
 
SpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at RuntimeSpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at Runtimeandrehoraa
 
Effectively Troubleshoot 9 Types of OutOfMemoryError
Effectively Troubleshoot 9 Types of OutOfMemoryErrorEffectively Troubleshoot 9 Types of OutOfMemoryError
Effectively Troubleshoot 9 Types of OutOfMemoryErrorTier1 app
 
Exploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdf
Exploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdfExploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdf
Exploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdfkalichargn70th171
 
SensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving CarsSensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving CarsChristian Birchler
 
Strategies for using alternative queries to mitigate zero results
Strategies for using alternative queries to mitigate zero resultsStrategies for using alternative queries to mitigate zero results
Strategies for using alternative queries to mitigate zero resultsJean Silva
 
Real-time Tracking and Monitoring with Cargo Cloud Solutions.pptx
Real-time Tracking and Monitoring with Cargo Cloud Solutions.pptxReal-time Tracking and Monitoring with Cargo Cloud Solutions.pptx
Real-time Tracking and Monitoring with Cargo Cloud Solutions.pptxRTS corp
 

Dernier (20)

A healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfA healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdf
 
Enhancing Supply Chain Visibility with Cargo Cloud Solutions.pdf
Enhancing Supply Chain Visibility with Cargo Cloud Solutions.pdfEnhancing Supply Chain Visibility with Cargo Cloud Solutions.pdf
Enhancing Supply Chain Visibility with Cargo Cloud Solutions.pdf
 
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
 
Large Language Models for Test Case Evolution and Repair
Large Language Models for Test Case Evolution and RepairLarge Language Models for Test Case Evolution and Repair
Large Language Models for Test Case Evolution and Repair
 
SAM Training Session - How to use EXCEL ?
SAM Training Session - How to use EXCEL ?SAM Training Session - How to use EXCEL ?
SAM Training Session - How to use EXCEL ?
 
The Role of IoT and Sensor Technology in Cargo Cloud Solutions.pptx
The Role of IoT and Sensor Technology in Cargo Cloud Solutions.pptxThe Role of IoT and Sensor Technology in Cargo Cloud Solutions.pptx
The Role of IoT and Sensor Technology in Cargo Cloud Solutions.pptx
 
2024 DevNexus Patterns for Resiliency: Shuffle shards
2024 DevNexus Patterns for Resiliency: Shuffle shards2024 DevNexus Patterns for Resiliency: Shuffle shards
2024 DevNexus Patterns for Resiliency: Shuffle shards
 
Keeping your build tool updated in a multi repository world
Keeping your build tool updated in a multi repository worldKeeping your build tool updated in a multi repository world
Keeping your build tool updated in a multi repository world
 
Understanding Flamingo - DeepMind's VLM Architecture
Understanding Flamingo - DeepMind's VLM ArchitectureUnderstanding Flamingo - DeepMind's VLM Architecture
Understanding Flamingo - DeepMind's VLM Architecture
 
VK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web DevelopmentVK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web Development
 
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
 
Precise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalPrecise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive Goal
 
Post Quantum Cryptography – The Impact on Identity
Post Quantum Cryptography – The Impact on IdentityPost Quantum Cryptography – The Impact on Identity
Post Quantum Cryptography – The Impact on Identity
 
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
 
SpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at RuntimeSpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at Runtime
 
Effectively Troubleshoot 9 Types of OutOfMemoryError
Effectively Troubleshoot 9 Types of OutOfMemoryErrorEffectively Troubleshoot 9 Types of OutOfMemoryError
Effectively Troubleshoot 9 Types of OutOfMemoryError
 
Exploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdf
Exploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdfExploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdf
Exploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdf
 
SensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving CarsSensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving Cars
 
Strategies for using alternative queries to mitigate zero results
Strategies for using alternative queries to mitigate zero resultsStrategies for using alternative queries to mitigate zero results
Strategies for using alternative queries to mitigate zero results
 
Real-time Tracking and Monitoring with Cargo Cloud Solutions.pptx
Real-time Tracking and Monitoring with Cargo Cloud Solutions.pptxReal-time Tracking and Monitoring with Cargo Cloud Solutions.pptx
Real-time Tracking and Monitoring with Cargo Cloud Solutions.pptx
 

Scala - The Simple Building Blocks of Modular Programming

  • 1. Scala - The Simple Parts Martin Odersky Typesafe and EPFL
  • 2. 10 Years of Scala
  • 3. Grown Up? Scala’s user community is pretty large for its age group. And, despite it coming out of academia it is amazingly successful in practice. But Scala is also discussed more controversially than usual for a language at its stage of adoption. Why? 3
  • 4. Controversies Internal controversies: Different communities not agreeing what programming in Scala should be. External complaints: “Scala is too academic” “Scala has sold out to industry” “Scala’s types are too hard” “Scala’s types are not strict enough” “Scala is everything and the kitchen sink” Signs that we have not made clear enough what the essence of programming in Scala is. 4
  • 5. 1-5 The Picture So Far Agile, with lightweight syntax Object-Oriented Functional Safe and performant, with strong static tpying = scalable
  • 6. What is “Scalable”? • 1st meaning: “Growable” – can be molded into new languages by adding libraries (domain specific or general) See: “Growing a language” (Guy Steele, 1998) • 2nd meaning: “Enabling Growth” – can be used for small as well as large systems – allows for smooth growth from small to large. 6
  • 7. A Growable Language • Flexible Syntax • Flexible Types • User-definable operators • Higher-order functions • Implicits ... • Make it relatively easy to build new DSLs on top of Scala • And where this fails, we can always use the macro system (even though so far it’s labeled experimental) 7
  • 9. Growable = Good? In fact, it’s a double edged sword. • DSLs can fraction the user community (“The Lisp curse”) • Besides, no language is liked by everyone, no matter whether its a DSL or general purpose. • Host languages get the blame for the DSLs they embed. Growable is great for experimentation. But it demands conformity and discipline for large scale production use. 9
  • 10. A Language For Growth • Can start with a one-liner. • Can experiment quickly. • Can grow without fearing to fall off the cliff. • Scala deployments now go into the millions of lines of code. – Language works for very large programs – Tools are challenged (build times!) but are catching up. “A large system is one where you do not know that some of its components even exist” 10
  • 11. What Enables Growth? • Unique combination of Object/Oriented and Functional • Large systems rely on both. Object-Oriented Functional • Unfortunately, there’s no established term for this object/functional? 11
  • 12. 12 Would prefer it like this OOPFP
  • 13. But unfortunately it’s often more like this OOP FP  And that’s where we are  How many OOP people see FP How many FP people see OOP
  • 14. 1-14 A Modular Language! Large Systems Object-Oriented Functional Small Scripts = modular
  • 15. Modular Programming • Systems should be composed from modules. • Modules should be simple parts that can be combined in many ways to give interesting results. (Simple: encapsulates one functionality) But that’s old hat! – Should we go back to Modula-2? – Modula-2 was limited by the Von-Neumann Bottleneck (see John Backus’ Turing award lecture). – Today’s systems need richer types. 15
  • 16. FP is Essential for Modular Programming Read: “Why Functional Programming Matters” (John Hughes, 1985). Paraphrasing: “Functional Programming is good because it leads to modules that can be combined freely.” 16
  • 17. Functions and Modules • Functional does not always imply Modular. • Non-modular concepts in functional languages: – Haskell’s type classes – OCaml’s records and variants – Dynamic typing 17
  • 18. Objects and Modules • Object-oriented languages are in a sense the successors of classical modular languages. • But Object-Oriented does not always imply Modular either. • Non-modular concepts in OOP languages: – Smalltalk’s virtual image. – Monkey-patching – Mutable state makes transformations hard. – Weak composition facilities require external DI frameworks. – Weak decomposition facilities encourage mixing applications with domain logic. 18
  • 19. Scala – The Simple Parts Before discussing library modules, let’s start with the simple parts in the language itself. Here are seven simple building blocks that can be combined in many ways. Together, they cover much of Scala’s programming in the small. Warning: This is pretty boring. 19
  • 20. #1 Expressions Everything is an expression if (age >= 18) "grownup" else "minor" val result = try { val people = file.iterator people.map(_.age).max } catch { case ex: IOException => -1 } 20
  • 21. #2: Scopes • Everything can be nested. • Static scoping discipline. • Two name spaces: Terms and Types. • Same rules for each. 21
  • 22. #3: Patterns and Case Classes trait Expr case class Number(n: Int) extends Expr case class Plus(l: Expr, r: Expr) extends Expr def eval(e: Expr): Int = e match { case Number(n) => n case Plus(l, r) => eval(l) + eval(r) } Simple & flexible, even if a bit verbose. 22
  • 23. #4: Recursion • Recursion is almost always better than a loop. • Simple fallback: Tail-recursive functions • Guaranteed to be efficient @tailrec def loop(xs: List[T], ys: List[U]): Boolean = if (xs.isEmpty) ys.isEmpty else ys.nonEmpty && loop(xs.tail, ys.tail) 23
  • 24. #5: Function Values • Functions are values • Can be named or anonymous • Scope rules are correct. def isMinor(p: Person) = p.age < 18 val (minors, adults) = people.partition(isMinor) val infants = minors.filter(_.age <= 3) • (this one is pretty standard by now) 24
  • 25. #6 Collections • Extensible set of collection types • Uniform operations • Transforms instead of CRUD • Very simple to use 25
  • 26. Collection Objection “The type of map is ugly / a lie” 26
  • 27. Collection Objection “The type of map is ugly / a lie” 27
  • 28. Why CanBuildFrom? • Why not define it like this? class Functor[F[_], T] { def map[U](f: T => U): F[U] } • Does not work for arrays, since we need a class-tag to build a new array. • More generally, does not work in any case where we need some additional information to build a new collection. • This is precisely what’s achieved by CanBuildFrom. 28
  • 29. Collection Objection “Set is not a Functor” WAT? Let’s check Wikipedia: (in fact, Set is in some sense the canonical functor!) 29
  • 30. Who’s right, Mathematics or Haskell? Crucial difference: – In mathematics, a type comes with an equality – In programming, a single conceptual value might have more than one representation, so equality has to be given explicitly. – If we construct a set, we need an equality operation to avoid duplicates. – In Haskell this is given by a typeclass Eq. – But the naive functor type def map[U](f: T => U): F[U] does not accommodate this type class. So the “obvious” type signature for `map` is inadequate. Again, CanBuildFrom fixes this. 30
  • 31. #7 Vars • Are vars not anti-modular? • Indeed, global state often leads to hidden dependencies. • But used-wisely, mutable state can cut down on annoying boilerplate and increase clarity. 31
  • 32. Comparing Compilers scalac: Mixed imperative/functional – Started from a Java codebase. – Mutable state is over-used. – Now we know better. dotc: Mostly functional architecture. State is used for – caching lazy vals, memoized functions, interned names, LRU caches. – persisting once a value is stable, store it in an object. – copy on write avoid copying untpd.Tree to tpd.Tree. – fresh values fresh names, unique ids – typer state current constraint & current diagnostics (versioned, explorable). 32
  • 33. Why Not Use a Monad? The fundamentalist functional approach would mandate that typer state is represented as a monad. Instead of now: def typed(tree: untpd.Tree, expected: Type): tpd.Tree def isSubType(tp1: Type, tp2: Type): Boolean we’d write: def typed(tree: untpd.Tree, expected: Type): TyperState[tps.Tree] def isSubType(tp1: Type, tp2: Type): TyperState[Boolean] 33
  • 34. Why Not Use a Monad? Instead of now: if (isSubType(t1, t2) && isSubType(t2, t3)) result we’d write: for { c1 <- isSubType(t1, t2) c2 <- isSubType(t2, t3) if c1 && c2 } yield result Why would this be better? 34
  • 35. Scala’s Modular Roots Modula-2 First language I programmed intensively First language for which I wrote a compiler. Modula-3 Introduced universal subtyping Haskell Type classes  Implicits SML modules Object ≅ Structure Class ≅ Functor Trait ≅ Signature Abstract Type ≅ Abstract Type Refinement ≅ Sharing Constraint 35
  • 36. Features Supporting Modular Programming 1. Rich types with functional semantics. – gives us the domains of discourse 2. Static typing. – Gives us the means to guarantee encapsulation – Read “On the Criteria for Decomposing Systems into Modules” (David Parnas, 1972) 3. Objects 4. Classes and Traits 36
  • 37. #5 Abstract Types trait Food class Grass extends Food trait Animal { type Diet <: Food def eat(food: Diet) } class Cow extends Animal with Food { type Diet = Grass def eat(food: Grass) = ??? } class Lion extends Animal { type Diet = Cow def eat(food: Cow) = ??? } 37
  • 38. #6 Parameterized Types class List[+T] class Set[T] class Function1[-T, +R] List[Number] Set[String] Function1[String, Int] Variance expressed by +/- annotations A good way to explain variance is by mapping to abstract types. 38
  • 39. Modelling Parameterized Types class Set[T] { ... }  class Set { type $T } Set[String]  Set { type $T = String } class List[+T] { ... }  class List { type $T }List[Number]  List { type $T <: Number } Parameters  Abstract members Arguments  Refinements
  • 40. #7 Implicit Parameters Implicit parameters are a simple concept But they are surprisingly versatile. Can represent a typeclass: def min(x: A, b: A)(implicit cmp: Ordering[A]): A 40
  • 41. #7 Implicit Parameters Can represent a context def typed(tree: untpd.Tree, expected: Type)(implicit ctx: Context): Type def compile(cmdLine: String) (implicit defaultOptions: List[String]): Unit Can represent a capability: def accessProfile(id: CustomerId) (implicit admin: AdminRights): Info 41
  • 42. Simple Parts Summary Language Expressions Scopes and Nesting Case Classes and Patterns Recursion Function Values Collections Vars A fairly modest and boring set of parts that can be combined in many ways. (Boring is good!) 42 Library Static Types Objects Classes Traits Abstract Types Type Parameters Implicit Parameters
  • 43. Thank You Follow us on twitter: @typesafe