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.

Fb8e986500c5059b2a6c0b2184bb0faf?s=128

Adelbert Chang

July 22, 2013
Tweet

Transcript

  1. Implicits With Great Power comes Great Responsibility Allen Chang Tuesday,

    July 23, 13
  2. Who am I? Tuesday, July 23, 13

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

    Santa Barbara Tuesday, July 23, 13
  4. 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
  5. 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
  6. 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
  7. 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
  8. Implicits Tuesday, July 23, 13

  9. Implicits •Double-edged sword Tuesday, July 23, 13

  10. Implicits •Double-edged sword •With great power comes great responsibility Tuesday,

    July 23, 13
  11. Implicits •Double-edged sword •With great power comes great responsibility •Superficial

    example: Tuesday, July 23, 13
  12. 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
  13. Superficial Example Tuesday, July 23, 13

  14. Superficial Example Tuesday, July 23, 13

  15. Superficial Example Tuesday, July 23, 13

  16. Superficial Example Tuesday, July 23, 13

  17. Superficial Example Tuesday, July 23, 13

  18. Superficial Example Tuesday, July 23, 13

  19. Superficial Example Tuesday, July 23, 13

  20. Superficial Example Tuesday, July 23, 13

  21. Superficial Example Tuesday, July 23, 13

  22. Superficial Example Tuesday, July 23, 13

  23. Superficial Example Tuesday, July 23, 13

  24. Superficial Example Tuesday, July 23, 13

  25. Places to mark implicit Tuesday, July 23, 13

  26. Places to mark implicit 1. Beginning of a parameter list

    Tuesday, July 23, 13
  27. Places to mark implicit 1. Beginning of a parameter list

    2. Variable or method definition Tuesday, July 23, 13
  28. 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
  29. 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
  30. 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
  31. 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
  32. Implicit Resolution Tuesday, July 23, 13

  33. Implicit Resolution How does implicit resolution work? Tuesday, July 23,

    13
  34. Implicit Resolution How does implicit resolution work? 1. Search local

    scope and imports Tuesday, July 23, 13
  35. Implicit Resolution How does implicit resolution work? 1. Search local

    scope and imports 2. Search companion objects of types Tuesday, July 23, 13
  36. Implicit Conversions Tuesday, July 23, 13

  37. •Let’s enrich a squared() method onto integers Implicit Conversions Tuesday,

    July 23, 13
  38. •Let’s enrich a squared() method onto integers Implicit Conversions Tuesday,

    July 23, 13
  39. Implicit Conversions •Let’s look at what is already available to

    Int Tuesday, July 23, 13
  40. Implicit Conversions Tuesday, July 23, 13

  41. Implicit Conversions Tuesday, July 23, 13

  42. Implicit Conversions •Q: How?? Tuesday, July 23, 13

  43. Implicit Conversions •Q: How?? •A: Implicit conversions! Tuesday, July 23,

    13
  44. Implicit Conversions Tuesday, July 23, 13

  45. Implicit Conversions Tuesday, July 23, 13

  46. Implicit Conversions Tuesday, July 23, 13

  47. Implicit Conversions Tuesday, July 23, 13

  48. Implicit Conversions Tuesday, July 23, 13

  49. Implicit Conversions Tuesday, July 23, 13

  50. Implicit Conversions 1. No “abs” method for Int Tuesday, July

    23, 13
  51. Implicit Conversions 1. No “abs” method for Int 2. We

    can convert an Int to a RichInt. Tuesday, July 23, 13
  52. 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
  53. •Let’s add a squared() method to integers Enrich My Library

    Tuesday, July 23, 13
  54. •Let’s add a squared() method to integers Enrich My Library

    1. No “squared” method for Int Tuesday, July 23, 13
  55. •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
  56. •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
  57. •Let’s add a squared() method to integers Enrich My Library

    Tuesday, July 23, 13
  58. •Let’s add a squared() method to integers Enrich My Library

    Tuesday, July 23, 13
  59. Enrich My Library Tuesday, July 23, 13

  60. •Be careful of “pimped” method names! Enrich My Library Tuesday,

    July 23, 13
  61. •Be careful of “pimped” method names! Enrich My Library Tuesday,

    July 23, 13
  62. •Be careful of “pimped” method names! Enrich My Library Tuesday,

    July 23, 13
  63. •Be careful of “pimped” method names! Enrich My Library Tuesday,

    July 23, 13
  64. •Be careful of “pimped” method names! Enrich My Library In

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

    scala.runtime.RichInt Tuesday, July 23, 13
  66. BigInt: Scala vs. Java Tuesday, July 23, 13

  67. BigInt: Scala vs. Java Tuesday, July 23, 13

  68. BigInt: Scala vs. Java Tuesday, July 23, 13

  69. BigInt: Scala vs. Java Tuesday, July 23, 13

  70. BigInt: Scala vs. Java Tuesday, July 23, 13

  71. BigInt: Scala vs. Java Tuesday, July 23, 13

  72. BigInt: Scala vs. Java Tuesday, July 23, 13

  73. Java Collections in Scala Tuesday, July 23, 13

  74. Java Collections in Scala Tuesday, July 23, 13

  75. Java Collections in Scala Tuesday, July 23, 13

  76. Java Collections in Scala Tuesday, July 23, 13

  77. Java Collections in Scala Tuesday, July 23, 13

  78. Java Collections in Scala Tuesday, July 23, 13

  79. Java Collections in Scala Tuesday, July 23, 13

  80. View Bounds •Make the type system work for you! Tuesday,

    July 23, 13
  81. View Bounds •Make the type system work for you! Tuesday,

    July 23, 13
  82. View Bounds •Make the type system work for you! Tuesday,

    July 23, 13
  83. View Bounds •Make the type system work for you! Tuesday,

    July 23, 13
  84. View Bounds •You are able to implicitly convert an A

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

    to an Ordered[A] Tuesday, July 23, 13
  86. 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
  87. 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
  88. Context Bounds Tuesday, July 23, 13

  89. Context Bounds •View bounds Tuesday, July 23, 13

  90. Context Bounds •View bounds •You are able to implicitly convert

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

    an A to a SomeClass[A] •Context bounds Tuesday, July 23, 13
  92. 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
  93. 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
  94. Context Bounds •scala.math.Ordered •A trait for data that have a

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

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

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

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

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

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

    from Haskell) •Typeclasses allow for ad-hoc polymorphism Tuesday, July 23, 13
  101. Context Bounds Tuesday, July 23, 13

  102. Context Bounds Tuesday, July 23, 13

  103. Context Bounds Tuesday, July 23, 13

  104. Context Bounds •Used most often for the “typeclass” pattern (borrowed

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

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

    from Haskell) •Typeclasses allow for ad-hoc polymorphism Tuesday, July 23, 13
  107. Enrich My Library, Revisited Tuesday, July 23, 13

  108. Enrich My Library, Revisited Tuesday, July 23, 13

  109. Enrich My Library, Revisited Tuesday, July 23, 13

  110. Enrich My Library, Revisited Tuesday, July 23, 13

  111. 1. No “squared” method for Int, Double, or BigInt Enrich

    My Library, Revisited Tuesday, July 23, 13
  112. 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
  113. 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
  114. 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
  115. Enrich My Library, Revisited Tuesday, July 23, 13

  116. Enrich My Library, Revisited Tuesday, July 23, 13

  117. Enrich My Library, Revisited Tuesday, July 23, 13

  118. Enrich My Library, Revisited Tuesday, July 23, 13

  119. Enrich My Library, Revisited Tuesday, July 23, 13

  120. comparePair •scala.math.Ordered •A trait for data that have a single

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

    natural ordering Tuesday, July 23, 13
  122. comparePair Tuesday, July 23, 13

  123. comparePair Tuesday, July 23, 13

  124. comparePair Tuesday, July 23, 13

  125. comparePair Tuesday, July 23, 13

  126. EOF @adelbertchang adelbertc@gmail.com Tuesday, July 23, 13

  127. EOF @adelbertchang adelbertc@gmail.com Questions? Tuesday, July 23, 13