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. 2.
  2. 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
  3. 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
  4. 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:
  5. 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
  6. 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
  7. 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
  8. 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
  9. 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
  10. 16.

    • Found hole: _ :: [Char] -> IO () In

    the expression: do _ "hello, world" Valid substitutions include ps :: String -> IO () putStrLn :: String -> IO () putStr :: String -> IO ()
  11. 18.

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

    Type holes (Haskell, Idris): ask the compiler what we need and what we have
  12. 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
  13. 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
  14. 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
  15. 22.

    EOF