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

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

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 ! [email protected] ✉ 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. Creating an alternate name for a type, and sometimes for

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

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

    package... Use Case: api usability ...you give users your dependencies for free
  7. 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
  8. ClassTag & TypeTag Easy access to Class and Type information

    without the runtime performance overhead Explanation:
  9. 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:
  10. The NoStackTrace Trait Explanation: Remember this? Huh? For use in

    scala.util.Failure and other places that require Throwables without necessarily needing stack traces
  11. 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
  12. 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?
  13. 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
  14. 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
  15. Context Bounds & Implicitly Explanation: You can add multiple context

    bounds and you can even compose them into higher order type classes
  16. @ImplicitNotFound Explanation: Remember this? When the compiler can’t find an

    implementation in implicit scope, it will complain in vague terms...
  17. 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....
  18. 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
  19. 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