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

Between Zero & Hero - Scala Tips and Tricks for the intermediate Scala developer

C2d2eb5dc036708dac0282dc78f88a03?s=47 Age Mooij
October 24, 2013

Between Zero & Hero - Scala Tips and Tricks for the intermediate Scala developer

I gave this updated version of my "Lessons Learned" talk at the Scala.IO conference in Paris on October 24th 2013. It tries to introduce some "just beyond beginner"-level Scala features in a quick-fire way. It ended up a bit too quick on the day :)


Age Mooij

October 24, 2013

More Decks by Age Mooij

Other Decks in Programming


  1. Between & ZERO HERO Tips & Tricks for the Intermediate-Level

    Scala Developer @agemooij ! age@scalapenos.com ✉ October 24th 2013
  2. The Scala Skillz Gap Zero Absolute beginners hero Martin Odersky

    Miles Sabin Viktor Klang Daniel Spiewak Etc. Target Audience ??? Me You? > 50% ?
  3. Agenda Type Aliases Classtag & typetag Auto-lifted Partial Functions NoStackTrace

    ! Type Classes Context Bounds & implicitly @implicitnotfound low priority default implicits Tips { Tricks {
  4. Type Aliases Tip:

  5. Creating an alternate name for a type, and sometimes for

    its companion object “What would that be good for?” You: Definition: Type Aliases
  6. Type Aliases When your API refers to external types... ...you

    force your users to always have to import those types Use Case: api usability
  7. Type Aliases By defining some type aliases in your base

    package... Use Case: api usability ...you give users your dependencies for free
  8. Use Case: simplification Type Aliases Sometimes type signatures get in

    the way of understanding A few well-placed type aliases can hide that complexity And it saves a lot of typing too
  9. Use Case: everywhere! Type Aliases

  10. Tip: ClassTag & TypeTag Reflection without the Performance Tax Mini

  11. ClassTag & TypeTag Easy access to Class and Type information

    without the runtime performance overhead Explanation:
  12. Auto-lifted Partial Functions Tip: You’re Probably Already Using This... Mini

  13. Explanation: Auto-lifted Partial Functions When reading method signatures such as

    these: You would think that you need to do this: But in fact Scala allows you to do this:
  14. Example: Auto-lifted Partial Functions Don’t do this:

  15. Auto-lifted Partial Functions Example: Do this:

  16. NoStackTrace Tip: Exceptions without the Performance Tax Mini

  17. The NoStackTrace Trait Explanation: Remember this? Huh? For use in

    scala.util.Failure and other places that require Throwables without necessarily needing stack traces
  18. Type Classes Trick: Demystified Mystic

  19. Type Classes Definition: "A type of Adapter that uses Scala’s

    implicits to add some extra capabilities to an existing type without direct coupling" “Sort of like @Autowire for types...” - Some guy on the internet - Me
  20. Type Classes Problem: It would be nice if there were

    an easy way to constrain T to something that can be (de)serialized or indexed without forcing users to extend their domain classes from my traits How do I turn a T into a RiakValue?
  21. Type Classes These are just regular Scala traits! Step 1:

    define some useful traits
  22. Type Classes Making the parameter implicit is (most of) the

    big trick that turns RiakSerializer into a type class Step 2: use them as implicit parameters
  23. Context Bounds & Implicitly Trick: Mini Making Type Classes a

    little Prettier
  24. Context Bounds & Implicitly Explanation: Implicit parameters that look like

    this: Can also be written like this: You can always get a reference to any implicit value or parameter using the implicitly[T] function
  25. Context Bounds & Implicitly Explanation: You can add multiple context

    bounds and you can even compose them into higher order type classes
  26. @ImplicitNotFound Trick: Think of Your Users! Mini

  27. @ImplicitNotFound Explanation: Remember this? When the compiler can’t find an

    implementation in implicit scope, it will complain in vague terms...
  28. @ImplicitNotFound Explanation: The compiler errors can sometimes be a little

    confusing to unsuspecting developers...
  29. @ImplicitNotFound Explanation: Ah, much better!

  30. @ImplicitNotFound Explanation: Happier, less confused users

  31. Low Priority Default Implicits Trick: Making Type Classes Actually Useful

  32. Why? Low Priority Default Implicits You want to provide some

    default implementations of your type classes so your users don’t have to do all the work. The companion object for your type class is the perfect place for this, but....
  33. Problem: Low Priority Default Implicits But you don't want your

    users to run into these kinds of problems when they want to override your defaults
  34. Solution: Use the Scala rules for resolving implicits to your

    advantage by making sure your defaults have the lowest possible implicit resolution priority This is a very common trick in Scala libraries Low Priority Default Implicits
  35. Reference: http://eed3si9n.com/revisiting-implicits-without-import-tax http://docs.scala-lang.org/tutorials/FAQ/finding-implicits.html Low Priority Default Implicits

  36. Low Priority Default Implicits Even Predef.scala has them!

  37. http://www.slideshare.net/DerekWyatt1/scala-implicits-not-to-be-feared http://danielwestheide.com/blog/2013/02/06/the-neophytes-guide-to-scala-part-12-type-classes.html Type Classes Further Reading:

  38. Thank You! QUESTIONS? @agemooij ! age@scalapenos.com ✉ October 24th 2013