SlideShare a Scribd company logo
What to Leave Implicit
Martin Odersky
Curry On and PLDI
Barcelona, June 2017
Con-textual
what comes with the text,
but is not in the text
Context is all around us
• the current configuration
• the current scope
• the meaning of “<” on this type
• the user on behalf of which the
operation is performed
• the security level in effect
• …
• globals
rigid if immutable,
unsafe if mutable
• monkey patching
• dependency injection
at runtime (Spring, Guice)
or with macros (MacWire)
• cake pattern
close coupling + recursion
Traditional ways to express context
“Parameterize all the things”
The Functional Way
- no side effects
- type safe
- fine-grained control
Functional is Good
- sea of parameters
- most of which hardly ever change
- repetitive, boring, prone to mistakes
But sometimes it’s too much of a
good thing …
If passing a lot of parameters gets tedious,
leave some of them implicit.
A more direct approach
Implicits
Types
ObjectsFunctions
Essence of Scala
Synthesis
If there’s one feature
that makes Scala
“Scala”, I would pick
implicits.
• takes you through some common uses of implicits,
and some interesting new ones,
• goes through a set of language changes that will
make implicits even more powerful and safer to use.
This Talk
t
• If you do not give an argument to an implicit parameter,
one will be provided for you.
• Eligible are all implicit values that are visible at the
point of call.
• If there are more than one eligible candidate, the most
specific one is chosen.
• If there’s no unique most specific candidate, an
ambiguity error Is reported.
Ground Rules
• are a cousin of implicit parameters.
• If the type A of an expression does not match the
expected type B …
… the compiler tries to find an implicit conversion
method from A to B.
• Same rules as for implicit parameters apply.
Implicit Conversions
• … implicit conversions came first in Scala.
• Original motivation: Provide a way for a class to
implement new interfaces.
• Implicit conversions were at first adopted
enthusiastically, but nowadays are mostly discouraged.
• I will ignore them in the rest of this talk.
Historically …
Implicit parameters can
• prove theorems
• establish context
• set configurations
• inject dependencies
• model capabilities
• implement type classes
Implicit Parameters - Use Cases
Curry Howard isomorphism:
Types = Theorems
Programs = Proofs
C.f. Kennedy & Russo: “Generalized Type Constraints”, OOPSLA 2004
Prove Theorems
Establish Context
Example: conference management system.
Reviewers should only see (directly or indirectly) the
scores of papers where they have no conflict with an
author.
Establish Context
Example: conference management system.
Context is usually stable, can change at specific points.
Configuration &
Dependency Management
are special cases of context passing.
see also: Dick Wall: The parfait pattern
Implement Type Classes
Implement Type Classes
Some changes to implicits foreseen for Scala 3:
1. Stricter distinction between parameters and
conversions
2. Implicit by-name parameters
3. Multiple implicit parameter lists
4. Implicit function types
New Developments
Multiple Implicit Parameter Lists
Problem: Implicit parameters are currently a bit irregular
compared to normal parameters:
• there can be only one implicit parameter section
• and it must come last.
This leads to some awkward workarounds (c.f. Aux
pattern).
Related problem: It’s sometimes confusing when a
parameter is implicit or explicit.
Proposal:
• Allow multiple implicit parameter lists
• Implicit and explicit parameter lists can be mixed freely.
• Explicit application of an implicit parameter must be
marked with a new “magic” method, explicitly.
Multiple Implicit Parameter Lists
Implicit Function Types
Let’s look at the conference management system again
Can we do better?
Having to write
a couple of times does not look so bad.
But in dotc there are > 2600 occurrences of the parameter
Would it not be nice to get rid of them?
Towards a solution
Let’s massage the definition of viewRankings a bit:
What is its type?
So far: Viewers => List[Paper]
From now on: implicit Viewers => List[Paper]
Two Rules for Typing
1. Implicit functions get implicit arguments just like implicit
methods. Given:
f expands to f(a).
2. Implicit functions get created on demand. If the
expected type of b is implicit A => B, then
b expands to implicit (_: A) => b
Revised Example
Assume:
Then reformulate:
Efficiency
Implicit function result types can be optimized
Instead of creating a closure like this:
we can simply create a curried function like this:
This brings the cost of implicit functions down to simple
implicit parameters.
• The reader monad is a somewhat popular method to
pass context.
• Essentially, it wraps the implicit reading in a monad.
• One advantage: The reading is abstracted in a type.
• But I believe this is shooting sparrows with cannons.
• Monads are about sequencing, they have have nothing
to do with passing context.
The Reader Monad
• allow the same conciseness as the reader monad,
• don’t force you into monadic style with explicit
sequencing,
• are fully composable,
• are more than 7x faster than the reader monad.
Implicit Function Types
Neat way to define structure-building DSLs, like this:
Natively supported in Groovy and in Kotlin via “receiver
functions”.
An Encore: The Builder Pattern
Scala Implementation
A Formalization
Bidirectional typing rules:
Γ ⊢ t :▷ T (Synthesis)
Γ ⊢ t ◁: T (Checking)
SI Typing Rules: Functions
SI Typing Rules: Variables
SI Typing Rules: Let
SI Typing Rules: Everything
Translation to System F
• Implicit parameters in Haskell (Lewis et al, 2000)
• Modular implicits in OCaml (White, Bour, Yallop, 2015)
• Agda’s implicit instances (Devriese and Piessens, 2011)
- very similar to implicit function types.
• Implicit Calculus by (Olivera, Schrjvers, Wadler et al.,
2012, 2017)
- a bit further removed from Scala than SI.
- Foundations of Implicit Function Types (Odersky,
Biboudis, Liu, Blanvillain) EPFL Report 229203, 2017
References
Dotty: Dmitry Petrashko Nicolas Stucki
Guillaume Martres Felix Mulder
Ondrej Lhotak Aggelos Biboudis
Liu Fengyun Vera Salvis
Olivier Blanvillain Enno Runne
Sebastien Douraene … and many others
DOT: Nada Amin Tiark Rompf
Sandro Stucki Samuel Grütter
scalac at Adriaan Moors Seth Tisue
Lightbend: Jason Zaugg Stefan Zeiger
Lukas Rytz
Scala Heather Miller Julien Richard-Foy
Center: Jorge Cantero Olafur Geirsson
Guillaume Massé Martin Duheim
Travis Lee
Credits
Thank You

More Related Content

PPTX
What To Leave Implicit
PDF
Simplicitly
PDF
Preparing for Scala 3
PPTX
The Evolution of Scala
PDF
From DOT to Dotty
PDF
PDF
Martin Odersky - Evolution of Scala
PPTX
flatMap Oslo presentation slides
What To Leave Implicit
Simplicitly
Preparing for Scala 3
The Evolution of Scala
From DOT to Dotty
Martin Odersky - Evolution of Scala
flatMap Oslo presentation slides

What's hot (18)

PPTX
Introduction to Scala
PPT
PPTX
Introduction to Scala
PPTX
Scala - The Simple Parts, SFScala presentation
PPT
Scala Talk at FOSDEM 2009
PDF
Quick introduction to scala
PDF
Scala : language of the future
PPTX
Advanced Functional Programming in Scala
KEY
Scala: functional programming for the imperative mind
PDF
Scala Days NYC 2016
PDF
Introduction to Functional Programming with Scala
PPTX
Scala basic
PDF
Introduction to Scala
ZIP
Why Scala for Web 2.0?
PDF
A Field Guide to DSL Design in Scala
PPTX
Scala, Play 2.0 & Cloud Foundry
PPT
Scala Days San Francisco
PDF
Functional programming in scala
Introduction to Scala
Introduction to Scala
Scala - The Simple Parts, SFScala presentation
Scala Talk at FOSDEM 2009
Quick introduction to scala
Scala : language of the future
Advanced Functional Programming in Scala
Scala: functional programming for the imperative mind
Scala Days NYC 2016
Introduction to Functional Programming with Scala
Scala basic
Introduction to Scala
Why Scala for Web 2.0?
A Field Guide to DSL Design in Scala
Scala, Play 2.0 & Cloud Foundry
Scala Days San Francisco
Functional programming in scala
Ad

Similar to What To Leave Implicit (20)

PPTX
Principled And Clean Coding
PPTX
Clean code quotes - Citações e provocações
KEY
Java Closures
PDF
JSR 335 / java 8 - update reference
PPTX
CPP19 - Revision
PDF
Working With Concurrency In Java 8
PDF
Spec: a lisp-flavoured type system
PDF
FP Days: Down the Clojure Rabbit Hole
PDF
EuroAD 2021: ChainRules.jl
PPTX
Design p atterns
PPTX
Introduction to Software - Coder Forge - John Mulhall
PPTX
Functional-style control flow in F#
PPTX
2CPP11 - Method Overloading
PPTX
Functional programming for the Advanced Beginner
PPTX
2CPP04 - Objects and Classes
PPTX
2CPP02 - C++ Primer
PPTX
Improving Software Quality Using Object Oriented Design Principles
PPTX
CPP16 - Object Design
PDF
Fantastic Design Patterns and Where to use them No Notes.pdf
PPTX
Distributed Model Validation with Epsilon
Principled And Clean Coding
Clean code quotes - Citações e provocações
Java Closures
JSR 335 / java 8 - update reference
CPP19 - Revision
Working With Concurrency In Java 8
Spec: a lisp-flavoured type system
FP Days: Down the Clojure Rabbit Hole
EuroAD 2021: ChainRules.jl
Design p atterns
Introduction to Software - Coder Forge - John Mulhall
Functional-style control flow in F#
2CPP11 - Method Overloading
Functional programming for the Advanced Beginner
2CPP04 - Objects and Classes
2CPP02 - C++ Primer
Improving Software Quality Using Object Oriented Design Principles
CPP16 - Object Design
Fantastic Design Patterns and Where to use them No Notes.pdf
Distributed Model Validation with Epsilon
Ad

More from Martin Odersky (8)

PPTX
Evolving Scala, Scalar conference, Warsaw, March 2025
PDF
scalar.pdf
PPTX
Capabilities for Resources and Effects
PDF
Implementing Higher-Kinded Types in Dotty
PPTX
Compilers Are Databases
PPTX
PPT
Oscon keynote: Working hard to keep it simple
PDF
Scala eXchange opening
Evolving Scala, Scalar conference, Warsaw, March 2025
scalar.pdf
Capabilities for Resources and Effects
Implementing Higher-Kinded Types in Dotty
Compilers Are Databases
Oscon keynote: Working hard to keep it simple
Scala eXchange opening

Recently uploaded (20)

PPTX
Operating system designcfffgfgggggggvggggggggg
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PDF
PTS Company Brochure 2025 (1).pdf.......
PDF
Understanding Forklifts - TECH EHS Solution
PPTX
ManageIQ - Sprint 268 Review - Slide Deck
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PDF
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
PPTX
ai tools demonstartion for schools and inter college
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PPTX
Introduction to Artificial Intelligence
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PDF
AI in Product Development-omnex systems
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PDF
top salesforce developer skills in 2025.pdf
PPTX
Online Work Permit System for Fast Permit Processing
Operating system designcfffgfgggggggvggggggggg
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PTS Company Brochure 2025 (1).pdf.......
Understanding Forklifts - TECH EHS Solution
ManageIQ - Sprint 268 Review - Slide Deck
Odoo Companies in India – Driving Business Transformation.pdf
Which alternative to Crystal Reports is best for small or large businesses.pdf
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
ai tools demonstartion for schools and inter college
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
Introduction to Artificial Intelligence
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
Upgrade and Innovation Strategies for SAP ERP Customers
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
AI in Product Development-omnex systems
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
Design an Analysis of Algorithms I-SECS-1021-03
top salesforce developer skills in 2025.pdf
Online Work Permit System for Fast Permit Processing

What To Leave Implicit

  • 1. What to Leave Implicit Martin Odersky Curry On and PLDI Barcelona, June 2017
  • 2. Con-textual what comes with the text, but is not in the text
  • 3. Context is all around us • the current configuration • the current scope • the meaning of “<” on this type • the user on behalf of which the operation is performed • the security level in effect • …
  • 4. • globals rigid if immutable, unsafe if mutable • monkey patching • dependency injection at runtime (Spring, Guice) or with macros (MacWire) • cake pattern close coupling + recursion Traditional ways to express context
  • 5. “Parameterize all the things” The Functional Way
  • 6. - no side effects - type safe - fine-grained control Functional is Good
  • 7. - sea of parameters - most of which hardly ever change - repetitive, boring, prone to mistakes But sometimes it’s too much of a good thing …
  • 8. If passing a lot of parameters gets tedious, leave some of them implicit. A more direct approach
  • 9. Implicits Types ObjectsFunctions Essence of Scala Synthesis If there’s one feature that makes Scala “Scala”, I would pick implicits.
  • 10. • takes you through some common uses of implicits, and some interesting new ones, • goes through a set of language changes that will make implicits even more powerful and safer to use. This Talk t
  • 11. • If you do not give an argument to an implicit parameter, one will be provided for you. • Eligible are all implicit values that are visible at the point of call. • If there are more than one eligible candidate, the most specific one is chosen. • If there’s no unique most specific candidate, an ambiguity error Is reported. Ground Rules
  • 12. • are a cousin of implicit parameters. • If the type A of an expression does not match the expected type B … … the compiler tries to find an implicit conversion method from A to B. • Same rules as for implicit parameters apply. Implicit Conversions
  • 13. • … implicit conversions came first in Scala. • Original motivation: Provide a way for a class to implement new interfaces. • Implicit conversions were at first adopted enthusiastically, but nowadays are mostly discouraged. • I will ignore them in the rest of this talk. Historically …
  • 14. Implicit parameters can • prove theorems • establish context • set configurations • inject dependencies • model capabilities • implement type classes Implicit Parameters - Use Cases
  • 15. Curry Howard isomorphism: Types = Theorems Programs = Proofs C.f. Kennedy & Russo: “Generalized Type Constraints”, OOPSLA 2004 Prove Theorems
  • 16. Establish Context Example: conference management system. Reviewers should only see (directly or indirectly) the scores of papers where they have no conflict with an author.
  • 17. Establish Context Example: conference management system. Context is usually stable, can change at specific points.
  • 18. Configuration & Dependency Management are special cases of context passing. see also: Dick Wall: The parfait pattern
  • 21. Some changes to implicits foreseen for Scala 3: 1. Stricter distinction between parameters and conversions 2. Implicit by-name parameters 3. Multiple implicit parameter lists 4. Implicit function types New Developments
  • 22. Multiple Implicit Parameter Lists Problem: Implicit parameters are currently a bit irregular compared to normal parameters: • there can be only one implicit parameter section • and it must come last. This leads to some awkward workarounds (c.f. Aux pattern). Related problem: It’s sometimes confusing when a parameter is implicit or explicit.
  • 23. Proposal: • Allow multiple implicit parameter lists • Implicit and explicit parameter lists can be mixed freely. • Explicit application of an implicit parameter must be marked with a new “magic” method, explicitly. Multiple Implicit Parameter Lists
  • 24. Implicit Function Types Let’s look at the conference management system again
  • 25. Can we do better? Having to write a couple of times does not look so bad. But in dotc there are > 2600 occurrences of the parameter Would it not be nice to get rid of them?
  • 26. Towards a solution Let’s massage the definition of viewRankings a bit: What is its type? So far: Viewers => List[Paper] From now on: implicit Viewers => List[Paper]
  • 27. Two Rules for Typing 1. Implicit functions get implicit arguments just like implicit methods. Given: f expands to f(a). 2. Implicit functions get created on demand. If the expected type of b is implicit A => B, then b expands to implicit (_: A) => b
  • 29. Efficiency Implicit function result types can be optimized Instead of creating a closure like this: we can simply create a curried function like this: This brings the cost of implicit functions down to simple implicit parameters.
  • 30. • The reader monad is a somewhat popular method to pass context. • Essentially, it wraps the implicit reading in a monad. • One advantage: The reading is abstracted in a type. • But I believe this is shooting sparrows with cannons. • Monads are about sequencing, they have have nothing to do with passing context. The Reader Monad
  • 31. • allow the same conciseness as the reader monad, • don’t force you into monadic style with explicit sequencing, • are fully composable, • are more than 7x faster than the reader monad. Implicit Function Types
  • 32. Neat way to define structure-building DSLs, like this: Natively supported in Groovy and in Kotlin via “receiver functions”. An Encore: The Builder Pattern
  • 34. A Formalization Bidirectional typing rules: Γ ⊢ t :▷ T (Synthesis) Γ ⊢ t ◁: T (Checking)
  • 35. SI Typing Rules: Functions
  • 36. SI Typing Rules: Variables
  • 38. SI Typing Rules: Everything
  • 40. • Implicit parameters in Haskell (Lewis et al, 2000) • Modular implicits in OCaml (White, Bour, Yallop, 2015) • Agda’s implicit instances (Devriese and Piessens, 2011) - very similar to implicit function types. • Implicit Calculus by (Olivera, Schrjvers, Wadler et al., 2012, 2017) - a bit further removed from Scala than SI. - Foundations of Implicit Function Types (Odersky, Biboudis, Liu, Blanvillain) EPFL Report 229203, 2017 References
  • 41. Dotty: Dmitry Petrashko Nicolas Stucki Guillaume Martres Felix Mulder Ondrej Lhotak Aggelos Biboudis Liu Fengyun Vera Salvis Olivier Blanvillain Enno Runne Sebastien Douraene … and many others DOT: Nada Amin Tiark Rompf Sandro Stucki Samuel Grütter scalac at Adriaan Moors Seth Tisue Lightbend: Jason Zaugg Stefan Zeiger Lukas Rytz Scala Heather Miller Julien Richard-Foy Center: Jorge Cantero Olafur Geirsson Guillaume Massé Martin Duheim Travis Lee Credits Thank You