Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Implicits: With Great Power Comes Great Responsibility

Implicits: With Great Power Comes Great Responsibility

Implicits are a powerful feature of Scala, and this power must be wielded carefully. The talk with cover an introduction to implicits, "pimp my library", view bounds, and context bounds, touching lightly on typeclasses and finally handing off to Vlad to give a more detailed overview of the powerful typeclass pattern. No prior knowledge of implicits is required, but some familiarity with Scala may be needed to understand code samples in the slides.

Adelbert Chang

July 22, 2013
Tweet

More Decks by Adelbert Chang

Other Decks in Programming

Transcript

  1. Who am I? •4th year student •B.S./M.S. Computer Science •UC

    Santa Barbara •Research Assistant •Large-scale graphs •Cluster Computing Tuesday, July 23, 13
  2. Who am I? •4th year student •B.S./M.S. Computer Science •UC

    Santa Barbara •Research Assistant •Large-scale graphs •Cluster Computing •Engineering Analytics Intern @ Box Tuesday, July 23, 13
  3. Who am I? •4th year student •B.S./M.S. Computer Science •UC

    Santa Barbara •Research Assistant •Large-scale graphs •Cluster Computing •Engineering Analytics Intern @ Box •Scala for 1.5 years •Used extensively in my research Tuesday, July 23, 13
  4. Outline •Intro to Implicits •Places to mark implicit •Implicit Conversions/Enrich

    My Library •scala.math.BigInt vs. java.math.BigInteger •Java collections in Scala •View Bounds •Context Bounds Tuesday, July 23, 13
  5. Implicits •Double-edged sword •With great power comes great responsibility •Superficial

    example: •A function greet() that when given a name and a greeting, and it will print out a nice friendly message Tuesday, July 23, 13
  6. Places to mark implicit 1. Beginning of a parameter list

    2. Variable or method definition Tuesday, July 23, 13
  7. Parameter list •If a parameter list is marked implicit, it

    tells the compiler the list may be missing and open to “implicit resolution” Tuesday, July 23, 13
  8. Parameter list •If a parameter list is marked implicit, it

    tells the compiler the list may be missing and open to “implicit resolution” Tuesday, July 23, 13
  9. Variable/Method Def •If a variable or method definition is marked

    implicit, it tells the compiler it is available for “implicit resolution” Tuesday, July 23, 13
  10. Variable/Method Def •If a variable or method definition is marked

    implicit, it tells the compiler it is available for “implicit resolution” Tuesday, July 23, 13
  11. Implicit Resolution How does implicit resolution work? 1. Search local

    scope and imports 2. Search companion objects of types Tuesday, July 23, 13
  12. Implicit Conversions 1. No “abs” method for Int 2. We

    can convert an Int to a RichInt. Tuesday, July 23, 13
  13. Implicit Conversions 1. No “abs” method for Int 2. We

    can convert an Int to a RichInt. 3. RichInt has an abs method. Tuesday, July 23, 13
  14. •Let’s add a squared() method to integers Enrich My Library

    1. No “squared” method for Int Tuesday, July 23, 13
  15. •Let’s add a squared() method to integers Enrich My Library

    1. No “squared” method for Int 2. We can convert an Int to a SquaredInt Tuesday, July 23, 13
  16. •Let’s add a squared() method to integers Enrich My Library

    1. No “squared” method for Int 3. SquaredInt has a “squared” method 2. We can convert an Int to a SquaredInt Tuesday, July 23, 13
  17. •Be careful of “pimped” method names! Enrich My Library In

    scala.runtime.RichInt Tuesday, July 23, 13
  18. •Be careful of “pimped” method names! Enrich My Library In

    scala.runtime.RichInt Tuesday, July 23, 13
  19. View Bounds •You are able to implicitly convert an A

    to an Ordered[A] Tuesday, July 23, 13
  20. View Bounds •You are able to implicitly convert an A

    to an Ordered[A] Tuesday, July 23, 13
  21. View Bounds •You are able to implicitly convert an A

    to an Ordered[A] 1. No “<“ method on arbitrary type A Tuesday, July 23, 13
  22. View Bounds •You are able to implicitly convert an A

    to an Ordered[A] 1. No “<“ method on arbitrary type A 2. The type tells the compiler we can convert A to an Ordered[A] which DOES have “<“ Tuesday, July 23, 13
  23. Context Bounds •View bounds •You are able to implicitly convert

    an A to a SomeClass[A] Tuesday, July 23, 13
  24. Context Bounds •View bounds •You are able to implicitly convert

    an A to a SomeClass[A] •Context bounds Tuesday, July 23, 13
  25. Context Bounds •View bounds •You are able to implicitly convert

    an A to a SomeClass[A] •Context bounds •You have an implicit value of SomeClass[A] Tuesday, July 23, 13
  26. Context Bounds •View bounds •You are able to implicitly convert

    an A to a SomeClass[A] •Context bounds •You have an implicit value of SomeClass[A] What’s the difference? Tuesday, July 23, 13
  27. Context Bounds •scala.math.Ordered •A trait for data that have a

    single natural ordering Tuesday, July 23, 13
  28. Context Bounds •scala.math.Ordered •A trait for data that have a

    single natural ordering Tuesday, July 23, 13
  29. Context Bounds •scala.math.Ordered •A trait for data that have a

    single natural ordering Tuesday, July 23, 13
  30. Context Bounds •scala.math.Ordered •A trait for data that have a

    single natural ordering Tuesday, July 23, 13
  31. Context Bounds •scala.math.Ordered •A trait for data that have a

    single natural ordering Tuesday, July 23, 13
  32. Context Bounds •Used most often for the “typeclass” pattern (borrowed

    from Haskell) •Typeclasses allow for ad-hoc polymorphism Tuesday, July 23, 13
  33. Context Bounds •Used most often for the “typeclass” pattern (borrowed

    from Haskell) •Typeclasses allow for ad-hoc polymorphism Tuesday, July 23, 13
  34. Context Bounds •Used most often for the “typeclass” pattern (borrowed

    from Haskell) •Typeclasses allow for ad-hoc polymorphism Tuesday, July 23, 13
  35. Context Bounds •Used most often for the “typeclass” pattern (borrowed

    from Haskell) •Typeclasses allow for ad-hoc polymorphism Tuesday, July 23, 13
  36. Context Bounds •Used most often for the “typeclass” pattern (borrowed

    from Haskell) •Typeclasses allow for ad-hoc polymorphism Tuesday, July 23, 13
  37. 1. No “squared” method for Int, Double, or BigInt Enrich

    My Library, Revisited Tuesday, July 23, 13
  38. 1. No “squared” method for Int, Double, or BigInt 2a.

    Can we convert Int/Double/BigInt to a SquaredNumeric? Enrich My Library, Revisited Tuesday, July 23, 13
  39. 1. No “squared” method for Int, Double, or BigInt 2a.

    Can we convert Int/Double/BigInt to a SquaredNumeric? 2b. Yes, there are Numeric[Int/Double/BigInt] instances in scope! Enrich My Library, Revisited Tuesday, July 23, 13
  40. 1. No “squared” method for Int, Double, or BigInt 3.

    SquaredNumeric has a “squared” method. 2a. Can we convert Int/Double/BigInt to a SquaredNumeric? 2b. Yes, there are Numeric[Int/Double/BigInt] instances in scope! Enrich My Library, Revisited Tuesday, July 23, 13