Sometimes, Ints, Strings and Doubles are not enough: Int has to be greater than 0, String cannot be empty, and Double has to belong inside an interval. What if we could define these restrictions in compile-time? In this talk you will learn about Refined Types, seeing how we can enhance our type system, through the lens of ‘Refined’. Refined is a Scala library for refining types with type-level predicates which constrain the set of values described by the refined type.
Enhancing the type system
with Reﬁned Types.
Who am I?
● Software Engineer,
Searcher of perfect modularization,
Lover of Functional Languages
● The cool ones
Scala, Clojure, Elixir
● The "vintage" ones
Java, C#, Python, Ruby
Once upon a time...
(Actually, from ML-family languages)
which is strongly
related to code
Does it solve the problem?
● Java-ish solution
● Options everywhere
● Boilerplate validation code
A reﬁnement type is a type
endowed with a predicate which
is assumed to hold for any
element of the reﬁned type.
Reﬁnement types are thus
related to behavioral subtyping.
reﬁnement type =
base type + predicate
Macros explained: THE DARK PLAYGROUND
It makes the compiler work for us.
Easier testing, many tests become
● Boolean (True, False, And, Or)
● Char (Letter, Uppercase, Whitespace)
● Collection (Contains, Forall, MinSize)
● Numeric (Less, Greater, Positive, Interval)
● String (Regex, Url, Uuid)
● And many more:
Combining predicates: Github Handle
How is that useful in the real world?
Validation error messages could be
The compiler does the job
Uniform (with Either)
More precise deﬁnition, less tests