Presentation on how to chat with PDF using ChatGPT code interpreter
Real-World Scala Design Patterns
1. Between ZERO
&HERO
Tips & Tricks for the
Intermediate-Level Scala Developer
Formerly known as “Real-World Scala Design Patterns"
November 6th 2013
!@agemooij
✉ age@scalapenos.com
2. The Scala Skillz Gap
Target Audience
Zero
???
hero
Absolute
beginners
Me
You?
Martin Odersky
Miles Sabin
Viktor Klang
Daniel Spiewak
Etc.
> 50% ?
6. Type Aliases
Use Case: api usability
When your API refers to
external types...
...you force your users
(and yourself) to always
have to import those
types
7. Type Aliases
Use Case: api usability
By defining some type
aliases in your base
package...
...you give your users
(and yourself) these
dependencies for free
8. Type Aliases
Use Case: simplification
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
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"
- Some guy on the internet
“Sort of like @Autowire for types...”
- Me
20. Type Classes
Problem: How do I turn a T into a RiakValue?
It would be nice if there were an easy way to
constrain T to something that can be
(de)serialized without forcing users to extend
their domain classes from my traits
21. Type Classes
Step 1: define some useful traits
These are just regular Scala traits!
22. Type Classes
Step 2: use them as implicit parameters
Making the parameter implicit is (most of) the big trick that
turns RiakSerializer into a type class
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 to a type
32. Low Priority Default Implicits
Why?
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. Low Priority Default Implicits
Problem:
You don't want your users to run into these kinds of
problems when they want to override your defaults
34. Low Priority Default Implicits
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