Let the Scala compiler work with you

Let the Scala compiler work with you

Programming in some languages can feel like you’re working for the compiler - the type checker is naggy, the type system limiting, and much of your code is extraneous. This is backwards. The compiler should be working for you, helping you check your code, allowing you to express the abstractions you want, and enabling you to write clean, beautiful code. In Scala we are lucky to have such a compiler. In this talk we will explore a variety of techniques, libraries, and compiler plugins for Scala that demonstrate the utility of having a compiler that works for you.

Fb8e986500c5059b2a6c0b2184bb0faf?s=128

Adelbert Chang

March 23, 2017
Tweet

Transcript

  1. Let the Scala compiler work for with you Adelbert Chang

  2. None
  3. Type systems: communicating with the compiler ▶ The type system

    is our way of talking to the compiler
  4. Type systems: communicating with the compiler ▶ The type system

    is our way of talking to the compiler ▶ The more expressive the type system, the more we can say to the compiler
  5. Type systems: communicating with the compiler ▶ The type system

    is our way of talking to the compiler ▶ The more expressive the type system, the more we can say to the compiler ▶ The more the compiler knows, the more it can help
  6. Type systems: communicating with the compiler ▶ The type system

    is our way of talking to the compiler ▶ The more expressive the type system, the more we can say to the compiler ▶ The more the compiler knows, the more it can help ▶ Scala examples:
  7. Type systems: communicating with the compiler ▶ The type system

    is our way of talking to the compiler ▶ The more expressive the type system, the more we can say to the compiler ▶ The more the compiler knows, the more it can help ▶ Scala examples: ▶ Implicits and type classes - proof search
  8. Type systems: communicating with the compiler ▶ The type system

    is our way of talking to the compiler ▶ The more expressive the type system, the more we can say to the compiler ▶ The more the compiler knows, the more it can help ▶ Scala examples: ▶ Implicits and type classes - proof search ▶ Path-dependent types - computing types
  9. Type systems: communicating with the compiler ▶ The type system

    is our way of talking to the compiler ▶ The more expressive the type system, the more we can say to the compiler ▶ The more the compiler knows, the more it can help ▶ Scala examples: ▶ Implicits and type classes - proof search ▶ Path-dependent types - computing types ▶ Literal and singleton types
  10. Type systems: communicating with the compiler ▶ The type system

    is our way of talking to the compiler ▶ The more expressive the type system, the more we can say to the compiler ▶ The more the compiler knows, the more it can help ▶ Scala examples: ▶ Implicits and type classes - proof search ▶ Path-dependent types - computing types ▶ Literal and singleton types ▶ Refinement types and (G)ADTs - modeling domains
  11. Live coding!

  12. Example: Compiler Errors for Humans1 1http://elm-lang.org/blog/compiler-errors-for-humans

  13. Example: Compiler Errors for Humans1 Figure 1: https://blog.rust-lang.org/2016/08/10/Shape-of-errors-to-come.html 1http://elm-lang.org/blog/compiler-errors-for-humans

  14. Example: type holes2 2https://phabricator.haskell.org/D3361

  15. Example: type holes2 ps :: String -> IO () ps

    = putStrLn ps2 :: a -> IO () ps2 ignored = putStrLn "hello, world" main :: IO () main = _ "hello, world" 2https://phabricator.haskell.org/D3361
  16. • Found hole: _ :: [Char] -> IO () In

    the expression: do _ "hello, world" Valid substitutions include ps :: String -> IO () putStrLn :: String -> IO () putStr :: String -> IO ()
  17. Other examples ▶ Friendly compile errors (Elm, Rust, Dotty)

  18. Other examples ▶ Friendly compile errors (Elm, Rust, Dotty) ▶

    Type holes (Haskell, Idris): ask the compiler what we need and what we have
  19. Other examples ▶ Friendly compile errors (Elm, Rust, Dotty) ▶

    Type holes (Haskell, Idris): ask the compiler what we need and what we have ▶ Affine and linear types (Rust, ATS): track ownership and lifetime of variables
  20. Other examples ▶ Friendly compile errors (Elm, Rust, Dotty) ▶

    Type holes (Haskell, Idris): ask the compiler what we need and what we have ▶ Affine and linear types (Rust, ATS): track ownership and lifetime of variables ▶ Refinement types (Liquid Haskell): refine existing types with logical predicates
  21. Other examples ▶ Friendly compile errors (Elm, Rust, Dotty) ▶

    Type holes (Haskell, Idris): ask the compiler what we need and what we have ▶ Affine and linear types (Rust, ATS): track ownership and lifetime of variables ▶ Refinement types (Liquid Haskell): refine existing types with logical predicates ▶ Dependent types (Idris, Agda, Coq): types are values
  22. EOF