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 Slide

  2. View Slide

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

    View 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

    View 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

    View 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:

    View 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

    View 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

    View 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

    View Slide

  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

    View Slide

  11. Live coding!

    View Slide

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

    View Slide

  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

    View Slide

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

    View Slide

  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

    View Slide

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

    View Slide

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

    View Slide

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

    View 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

    View 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

    View Slide

  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

    View Slide

  22. EOF

    View Slide