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
Tweet

More Decks by Age Mooij

Other Decks in Programming

Transcript

  1. Between
    &
    ZERO
    HERO
    Tips & Tricks for the
    Intermediate-Level Scala Developer
    @agemooij
    !
    [email protected]

    October 24th 2013

    View Slide

  2. The Scala Skillz Gap
    Zero
    Absolute
    beginners
    hero
    Martin Odersky
    Miles Sabin
    Viktor Klang
    Daniel Spiewak
    Etc.
    Target Audience
    ???
    Me
    You?
    > 50% ?

    View Slide

  3. Agenda
    Type Aliases
    Classtag & typetag
    Auto-lifted Partial Functions
    NoStackTrace
    !
    Type Classes
    Context Bounds & implicitly
    @implicitnotfound
    low priority default implicits
    Tips
    {
    Tricks
    {

    View Slide

  4. Type Aliases
    Tip:

    View Slide

  5. Creating an alternate name for a type,
    and sometimes for its companion object
    “What would that be good for?”
    You:
    Definition:
    Type Aliases

    View Slide

  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

    View Slide

  7. Type Aliases
    By defining some type
    aliases in your base
    package...
    Use Case: api usability
    ...you give users your
    dependencies for free

    View Slide

  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

    View Slide

  9. Use Case: everywhere!
    Type Aliases

    View Slide

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

    View Slide

  11. ClassTag & TypeTag
    Easy access to Class and Type information without the runtime
    performance overhead
    Explanation:

    View Slide

  12. Auto-lifted Partial Functions
    Tip:
    You’re Probably Already Using This...
    Mini

    View Slide

  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:

    View Slide

  14. Example:
    Auto-lifted Partial Functions
    Don’t do this:

    View Slide

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

    View Slide

  16. NoStackTrace
    Tip:
    Exceptions without the Performance Tax
    Mini

    View Slide

  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

    View Slide

  18. Type Classes
    Trick:
    Demystified
    Mystic

    View Slide

  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

    View Slide

  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?

    View Slide

  21. Type Classes
    These are just regular Scala traits!
    Step 1:
    define some useful traits

    View Slide

  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

    View Slide

  23. Context Bounds & Implicitly
    Trick:
    Mini
    Making Type Classes a little Prettier

    View Slide

  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

    View Slide

  25. Context Bounds & Implicitly
    Explanation:
    You can add multiple context bounds and you can even
    compose them into higher order type classes

    View Slide

  26. @ImplicitNotFound
    Trick:
    Think of Your Users!
    Mini

    View Slide

  27. @ImplicitNotFound
    Explanation:
    Remember this?
    When the compiler can’t find an implementation in implicit
    scope, it will complain in vague terms...

    View Slide

  28. @ImplicitNotFound
    Explanation:
    The compiler errors can sometimes be a little confusing to
    unsuspecting developers...

    View Slide

  29. @ImplicitNotFound
    Explanation:
    Ah, much better!

    View Slide

  30. @ImplicitNotFound
    Explanation:
    Happier, less confused users

    View Slide

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

    View Slide

  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....

    View Slide

  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

    View Slide

  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

    View Slide

  35. Reference:
    http://eed3si9n.com/revisiting-implicits-without-import-tax
    http://docs.scala-lang.org/tutorials/FAQ/finding-implicits.html
    Low Priority Default Implicits

    View Slide

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

    View Slide

  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:

    View Slide

  38. Thank You!
    QUESTIONS?
    @agemooij
    !
    [email protected]

    October 24th 2013

    View Slide