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

Enhancing the type system with Refined Types

Enhancing the type system with Refined Types

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.

Juliano Alves

February 14, 2020
Tweet

More Decks by Juliano Alves

Other Decks in Programming

Transcript

  1. 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 @vonjuliano juliano-alves.com
  2. Does it solve the problem? • Java-ish solution • Options

    everywhere • Boilerplate validation code
  3. A refinement type is a type endowed with a predicate

    which is assumed to hold for any element of the refined type. … Refinement types are thus related to behavioral subtyping. https://en.wikipedia.org/wiki/Refinement_type
  4. Provided Predicates • 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: https://github.com/fthomas/refined
  5. Why Refined? The compiler does the job Typesafety Uniform (with

    Either) Validation Improves maintainability Expressiveness More precise definition, less tests Easier testing