SlideShare a Scribd company logo
Implicits
                 2 Features, 1 Keyword



Nick Partridge
@nkpart
2 Features


• Implicit Conversions
• Implicit Parameters
Conversions


• int a = 5.3f; // Implicit conversion
Conversions


• val a: A = ...;
• val b: B = a; // Compile Fail. Of course.
Conversions

• val a: A = ...;
• implicit def (a: A): B = ...;
• val b: B = a;
• // Profit.
Conversions

        Compiler searches the ‘implicit scope’

• val b: B = a;
• val b: B = bToA(a);
Pimp My Library
  Live Demo.
Conversions

• Also triggered by a method search
  case class Foo(s: String) {
      def pl = s + “es”
  }
Conversions

implicit def toFoo(s: String) = Foo(s)


“peach”.pl // == “peaches”


toFoo(s).pl
Not Transitive
      Implicit A => B
      Implicit B => C

Does not mean Implicit A => C
View Bounds
   Live coding!
The End. Of Part 1.
Parameters
Parameters

• Scala functions have parameter groups
 • def foo(a: Int)(b: Int)(c: Int) = ...
 • foo(1)(2)(3)
Parameters

• The *last* group can be `implicit`.
• def mungeData(d: Data)(implicit m: Munger)

• mungeData(myData)(myMunger)
Parameters


• implicit val myMunger = new Munger
• mungeData(myData)(myMunger)
   Compiler figures out the last group
Parameters

•   def mungeData[D](d: D)(implicit m: Munger[D])


•   mungeData(“foo”) // Compiler finds a
    Munger[String]
Parameters
• Higher kinds!
• def mungeData[M[_], D](d: D)(implicit m:
  Munger[D], each: Each[M]) = ...
• mungeData(List(1,2,3))
 • Munger[Int]
 • Each[List]
Live demo! Monoid!
Conversions
Parameters
Where to get the
‘implicits’ from?
Implicit Search Scope

• Similar to value resolution scope
 • local variables, values in current object,
    super classes.
  • Imported object members
  • Function parameters
Implicit Search Scope
// COMPANION OBJECTS
trait Monoid[T]
object Monoid { // SAME NAME
    implicit val s = new Monoid[String]
}
// `s` will be available anywhere a
Monoid[String] is needed. Without imports.
Implicit Search Scope

def foo[T](t: T)(implicit m: Monoid[T]) = {
    // m is available for any implicit
    parameters needed within this method
    body
}
More!
• Anonymous functions
 • implicit r => { ... }
• Import instances
 • case class Z(a: A) { implicit ia = a }
 • val z = new Z(someA)
 • import z._
Wonderful and Terrible.
La fin.

More Related Content

PPT
CPP Language Basics - Reference
PDF
Kotlin Starter Pack
PPTX
Lambda Expressions in C++
PPTX
function in c
PDF
Functional programming in kotlin with Arrow [Sunnytech 2018]
PDF
Functional Programming 101 with Scala and ZIO @FunctionalWorld
PDF
1 introducing c language
PPTX
Linq inside out
CPP Language Basics - Reference
Kotlin Starter Pack
Lambda Expressions in C++
function in c
Functional programming in kotlin with Arrow [Sunnytech 2018]
Functional Programming 101 with Scala and ZIO @FunctionalWorld
1 introducing c language
Linq inside out

What's hot (20)

PPTX
Function (rule in programming)
PDF
Blazing Fast, Pure Effects without Monads — LambdaConf 2018
PPTX
Learning C++ - Functions in C++ 3
PPT
Function overloading(C++)
PDF
The Next Great Functional Programming Language
PDF
Functions
PPTX
Function C++
PPT
Functions in C++
PPT
Lecture#6 functions in c++
PDF
Quark: A Purely-Functional Scala DSL for Data Processing & Analytics
PDF
Static type checking in python
PPT
CPU INPUT OUTPUT
PDF
03 function overloading
PDF
Scalapeno18 - Thinking Less with Scala
PDF
Introduction to programming with ZIO functional effects
PPTX
My lecture infix-to-postfix
PPTX
Loops in Python
PPT
16717 functions in C++
 
PPTX
C and C++ functions
PPTX
functions of C++
Function (rule in programming)
Blazing Fast, Pure Effects without Monads — LambdaConf 2018
Learning C++ - Functions in C++ 3
Function overloading(C++)
The Next Great Functional Programming Language
Functions
Function C++
Functions in C++
Lecture#6 functions in c++
Quark: A Purely-Functional Scala DSL for Data Processing & Analytics
Static type checking in python
CPU INPUT OUTPUT
03 function overloading
Scalapeno18 - Thinking Less with Scala
Introduction to programming with ZIO functional effects
My lecture infix-to-postfix
Loops in Python
16717 functions in C++
 
C and C++ functions
functions of C++
Ad

Similar to Scala implicits (20)

PPTX
Python for Data Science function third module ppt.pptx
ZIP
D言語をたまには真面目に紹介してみる
PPTX
Front end fundamentals session 1: javascript core
PPTX
CH.4FUNCTIONS IN C (1).pptx
PDF
Denis Lebedev, Swift
PPTX
Python_Functions_Unit1.pptx
PDF
Vim Script Programming
PDF
Python3
PPTX
Introduction to C++
PDF
Introduction to Python for Plone developers
PDF
Introduction to Functional Programming
PPTX
Functions
PPTX
C language
PPTX
UNIT-02-pythonfunctions python function using detehdjsjehhdjejdhdjdjdjddjdhdhhd
PDF
A Plan towards Ruby 3 Types
PDF
A Few of My Favorite (Python) Things
PPTX
Functions, List and String methods
PDF
What is Python.pdf
PPTX
Angular2 for Beginners
PPTX
Core java complete ppt(note)
Python for Data Science function third module ppt.pptx
D言語をたまには真面目に紹介してみる
Front end fundamentals session 1: javascript core
CH.4FUNCTIONS IN C (1).pptx
Denis Lebedev, Swift
Python_Functions_Unit1.pptx
Vim Script Programming
Python3
Introduction to C++
Introduction to Python for Plone developers
Introduction to Functional Programming
Functions
C language
UNIT-02-pythonfunctions python function using detehdjsjehhdjejdhdjdjdjddjdhdhhd
A Plan towards Ruby 3 Types
A Few of My Favorite (Python) Things
Functions, List and String methods
What is Python.pdf
Angular2 for Beginners
Core java complete ppt(note)
Ad

More from nkpart (6)

PPT
Introduction to Laws
KEY
Writing Better Haskell
KEY
Tools for writing Haskell programs
KEY
Tools for writing Haskell programs
KEY
Deriving Scalaz
KEY
Taming Errors with FunctionalKit
Introduction to Laws
Writing Better Haskell
Tools for writing Haskell programs
Tools for writing Haskell programs
Deriving Scalaz
Taming Errors with FunctionalKit

Recently uploaded (20)

PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
cuic standard and advanced reporting.pdf
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Empathic Computing: Creating Shared Understanding
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PPTX
sap open course for s4hana steps from ECC to s4
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Encapsulation theory and applications.pdf
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPTX
Big Data Technologies - Introduction.pptx
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPTX
Machine Learning_overview_presentation.pptx
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Approach and Philosophy of On baking technology
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
The Rise and Fall of 3GPP – Time for a Sabbatical?
cuic standard and advanced reporting.pdf
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Programs and apps: productivity, graphics, security and other tools
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Chapter 3 Spatial Domain Image Processing.pdf
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Empathic Computing: Creating Shared Understanding
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
sap open course for s4hana steps from ECC to s4
Building Integrated photovoltaic BIPV_UPV.pdf
Encapsulation theory and applications.pdf
Advanced methodologies resolving dimensionality complications for autism neur...
Big Data Technologies - Introduction.pptx
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Machine Learning_overview_presentation.pptx
Assigned Numbers - 2025 - Bluetooth® Document
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Approach and Philosophy of On baking technology
Build a system with the filesystem maintained by OSTree @ COSCUP 2025

Scala implicits