$30 off During Our Annual Pro Sale. View Details »

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. Enhancing the type system
    with Refined Types.
    Juliano Alves
    @vonjuliano
    juliano-alves.com

    View Slide

  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

    View Slide

  3. https://getquill.io

    View Slide

  4. View Slide

  5. View Slide

  6. Once upon a time...

    View Slide

  7. View Slide

  8. View Slide

  9. View Slide

  10. View Slide

  11. https://blog.codinghorror.com/new-programming-jargon/

    View Slide

  12. View Slide

  13. View Slide

  14. View Slide

  15. Tiny Types.

    View Slide

  16. View Slide

  17. View Slide

  18. (Actually, from ML-family languages)

    View Slide

  19. View Slide

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

    View Slide

  21. View Slide

  22. View Slide

  23. Does it solve the problem?
    ● Java-ish solution
    ● Options everywhere
    ● Boilerplate validation code

    View Slide

  24. Refinement types.

    View Slide

  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

    View Slide

  26. View Slide

  27. refinement type =
    base type + predicate

    View Slide

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

    View Slide

  29. View Slide

  30. base type
    +
    predicate

    View Slide

  31. View Slide

  32. View Slide

  33. View Slide

  34. Macros explained: THE DARK PLAYGROUND

    View Slide

  35. View Slide

  36. View Slide

  37. Domain reduction.

    View Slide

  38. It makes the compiler work for us.

    View Slide

  39. Easier testing, many tests become
    irrelevant.

    View Slide

  40. Provided Predicates.

    View Slide

  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

    View Slide

  42. Using Predicates.

    View Slide

  43. Combining predicates: Github Handle

    View Slide

  44. View Slide

  45. How is that useful in the real world?

    View Slide

  46. RefinedTypeOps

    View Slide

  47. Drawbacks.

    View Slide

  48. Compile time.

    View Slide

  49. Validation error messages could be
    better.

    View Slide

  50. Summary

    View Slide

  51. Why Refined?
    The compiler does the job
    Typesafety
    Uniform (with Either)
    Validation
    Improves maintainability
    Expressiveness
    More precise definition, less tests
    Easier testing

    View Slide

  52. Thank you!
    @vonjuliano
    juliano-alves.com/

    View Slide