Your SlideShare is downloading. ×
Scala implicits
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Scala implicits

1,144

Published on

Introduction to

Introduction to

0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,144
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
0
Comments
0
Likes
3
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide



























  • Transcript

    • 1. Implicits 2 Features, 1 Keyword Nick Partridge @nkpart
    • 2. 2 Features • Implicit Conversions • Implicit Parameters
    • 3. Conversions • int a = 5.3f; // Implicit conversion
    • 4. Conversions • val a: A = ...; • val b: B = a; // Compile Fail. Of course.
    • 5. Conversions • val a: A = ...; • implicit def (a: A): B = ...; • val b: B = a; • // Profit.
    • 6. Conversions Compiler searches the ‘implicit scope’ • val b: B = a; • val b: B = bToA(a);
    • 7. Pimp My Library Live Demo.
    • 8. Conversions • Also triggered by a method search case class Foo(s: String) { def pl = s + “es” }
    • 9. Conversions implicit def toFoo(s: String) = Foo(s) “peach”.pl // == “peaches” toFoo(s).pl
    • 10. Not Transitive Implicit A => B Implicit B => C Does not mean Implicit A => C
    • 11. View Bounds Live coding!
    • 12. The End. Of Part 1.
    • 13. Parameters
    • 14. Parameters • Scala functions have parameter groups • def foo(a: Int)(b: Int)(c: Int) = ... • foo(1)(2)(3)
    • 15. Parameters • The *last* group can be `implicit`. • def mungeData(d: Data)(implicit m: Munger) • mungeData(myData)(myMunger)
    • 16. Parameters • implicit val myMunger = new Munger • mungeData(myData)(myMunger) Compiler figures out the last group
    • 17. Parameters • def mungeData[D](d: D)(implicit m: Munger[D]) • mungeData(“foo”) // Compiler finds a Munger[String]
    • 18. 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]
    • 19. Live demo! Monoid!
    • 20. Conversions Parameters
    • 21. Where to get the ‘implicits’ from?
    • 22. Implicit Search Scope • Similar to value resolution scope • local variables, values in current object, super classes. • Imported object members • Function parameters
    • 23. 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.
    • 24. Implicit Search Scope def foo[T](t: T)(implicit m: Monoid[T]) = { // m is available for any implicit parameters needed within this method body }
    • 25. More! • Anonymous functions • implicit r => { ... } • Import instances • case class Z(a: A) { implicit ia = a } • val z = new Z(someA) • import z._
    • 26. Wonderful and Terrible.
    • 27. La fin.

    ×