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

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.

Adelbert Chang

March 23, 2017
Tweet

More Decks by Adelbert Chang

Other Decks in Programming

Transcript

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

    View full-size slide

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

    View full-size slide

  3. 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

    View full-size slide

  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
    ▶ The more the compiler knows, the more it can help

    View full-size slide

  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
    ▶ Scala examples:

    View full-size slide

  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:
    ▶ Implicits and type classes - proof search

    View full-size slide

  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
    ▶ Path-dependent types - computing types

    View full-size slide

  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
    ▶ Literal and singleton types

    View full-size slide

  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
    ▶ Refinement types and (G)ADTs - modeling domains

    View full-size slide

  10. Live coding!

    View full-size slide

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

    View full-size slide

  12. 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

    View full-size slide

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

    View full-size slide

  14. 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

    View full-size slide

  15. • Found hole: _ :: [Char] -> IO ()
    In the expression:
    do _ "hello, world"
    Valid substitutions include
    ps :: String -> IO ()
    putStrLn :: String
    -> IO ()
    putStr :: String
    -> IO ()

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  18. 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

    View full-size slide

  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
    ▶ Refinement types (Liquid Haskell): refine existing types with logical
    predicates

    View full-size slide

  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
    ▶ Dependent types (Idris, Agda, Coq): types are values

    View full-size slide