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.

6b8d8f84c065846e8ec1f12f67d79991?s=128

Juliano Alves

February 14, 2020
Tweet

Transcript

  1. Enhancing the type system with Refined Types. Juliano Alves @vonjuliano

    juliano-alves.com
  2. 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
  3. https://getquill.io

  4. None
  5. None
  6. Once upon a time...

  7. None
  8. None
  9. None
  10. None
  11. https://blog.codinghorror.com/new-programming-jargon/

  12. None
  13. None
  14. None
  15. Tiny Types.

  16. None
  17. None
  18. (Actually, from ML-family languages)

  19. None
  20. Expressiveness, which is strongly related to code maintainability.

  21. None
  22. None
  23. Does it solve the problem? • Java-ish solution • Options

    everywhere • Boilerplate validation code
  24. Refinement types.

  25. 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
  26. None
  27. refinement type = base type + predicate

  28. https://github.com/fthomas/refined

  29. None
  30. base type + predicate

  31. None
  32. None
  33. None
  34. Macros explained: THE DARK PLAYGROUND

  35. None
  36. None
  37. Domain reduction.

  38. It makes the compiler work for us.

  39. Easier testing, many tests become irrelevant.

  40. Provided Predicates.

  41. 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
  42. Using Predicates.

  43. Combining predicates: Github Handle

  44. None
  45. How is that useful in the real world?

  46. RefinedTypeOps

  47. Drawbacks.

  48. Compile time.

  49. Validation error messages could be better.

  50. Summary

  51. Why Refined? The compiler does the job Typesafety Uniform (with

    Either) Validation Improves maintainability Expressiveness More precise definition, less tests Easier testing
  52. Thank you! @vonjuliano juliano-alves.com/