Slide 1

Slide 1 text

Between & ZERO HERO Tips & Tricks for the Intermediate-Level Scala Developer @agemooij ! [email protected] ✉ October 24th 2013

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

Type Aliases Tip:

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

Type Aliases When your API refers to external types... ...you force your users to always have to import those types Use Case: api usability

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

Use Case: everywhere! Type Aliases

Slide 10

Slide 10 text

Tip: ClassTag & TypeTag Reflection without the Performance Tax Mini

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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:

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

Auto-lifted Partial Functions Example: Do this:

Slide 16

Slide 16 text

NoStackTrace Tip: Exceptions without the Performance Tax Mini

Slide 17

Slide 17 text

The NoStackTrace Trait Explanation: Remember this? Huh? For use in scala.util.Failure and other places that require Throwables without necessarily needing stack traces

Slide 18

Slide 18 text

Type Classes Trick: Demystified Mystic

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

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?

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

@ImplicitNotFound Trick: Think of Your Users! Mini

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

@ImplicitNotFound Explanation: Ah, much better!

Slide 30

Slide 30 text

@ImplicitNotFound Explanation: Happier, less confused users

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

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

Slide 35

Slide 35 text

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

Slide 36

Slide 36 text

Low Priority Default Implicits Even Predef.scala has them!

Slide 37

Slide 37 text

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:

Slide 38

Slide 38 text

Thank You! QUESTIONS? @agemooij ! [email protected] ✉ October 24th 2013