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

Have your Sorbet and eat it too

1a45b192d0bbaf167afb43a41859e313?s=47 Ju Liu
September 09, 2019

Have your Sorbet and eat it too

A short and sweet introduction to Sorbet, a type checker for Ruby. Learn how you can use types to write bulletproof code!


Ju Liu

September 09, 2019


  1. Have your Sorbet and eat it too !!!!!!!!!!!!!!! 9th September

    LRUG - @arkh4m
  2. Hello! ! — My name is Ju — I work

    for NoRedInk — You can find me online as @arkh4m 9th September LRUG - @arkh4m 2
  3. What is Sorbet? ! — Sorbet is a type checker

    designed for Ruby — It was built by Stripe and open sourced this year — You can find it at sorbet.org and play with it at sorbet.run 9th September LRUG - @arkh4m 3
  4. Type checker you say? 9th September LRUG - @arkh4m 4

  5. Different types of Type Systems — Dynamic type systems —

    Static type systems 9th September LRUG - @arkh4m 5
  6. What To Know Before Debating Type Systems 9th September LRUG

    - @arkh4m 6
  7. What are static types systems good for? 9th September LRUG

    - @arkh4m 7
  8. 1. Correctness Imagine a world without NoMethodError: undefined method 9th

    September LRUG - @arkh4m 8
  9. 2. Tooling Great refactoring support, since the computer knows more

    about the program. 9th September LRUG - @arkh4m 9
  10. 3. Documentation Imagine a search engine where you say "Give

    me a function that takes a list of values, a function, an initial value and returns a result of the same type". It exists in Haskell and it's called Hoogle. 9th September LRUG - @arkh4m 10
  11. 4. Performance Just like tooling, if the compiler knows more

    about the code, it can optimize it too. 9th September LRUG - @arkh4m 11
  12. Does it actually work? 9th September LRUG - @arkh4m 12

  13. NoRedInk — 420'000 lines of Elm in production — 30

    engineers working in the team — Almost no runtime exceptions 9th September LRUG - @arkh4m 13
  14. Dropbox Once your project is tens of thousands of lines

    of code, and several engineers work on it, our experience tells us that understanding code becomes the key to maintaining developer productivity. Without type annotations, basic reasoning such as figuring out the valid arguments to a function, or the possible return value types, becomes a hard problem. 9th September LRUG - @arkh4m 14
  15. Dropbox A type checker solves this problem by providing a

    formal language for describing types, and by validating that the provided types match the implementation. In essence, it provides verified documentation. 9th September LRUG - @arkh4m 15
  16. Ok, show me the code 9th September LRUG - @arkh4m

  17. A simple example 9th September LRUG - @arkh4m 17

  18. # typed: true class Kitten extend T::Sig sig {params(x: Integer).returns(String)}

    def jump(x) "The kitten jumps #{x} times." end end Try it 9th September LRUG - @arkh4m 18
  19. If we wrote Kitten.new.jump(7) Sorbet would tell us No errors!

    Great job. 9th September LRUG - @arkh4m 19
  20. But if we wrote Kitten.new.jump("10") 9th September LRUG - @arkh4m

  21. We would get editor.rb:12: Expected Integer but found String("10") for

    argument x https://srb.help/7002 12 |Kitten.new.jump("10") ^^^^^^^^^^^^^^^^^^^^^ editor.rb:6: Method Kitten#jump has specified x as Integer 6 | sig {params(x: Integer).returns(String)} ^ Got String("10") originating from: editor.rb:12: 12 |Kitten.new.jump("10") ^^^^ Errors: 1 9th September LRUG - @arkh4m 21
  22. Or if we wrote Kitten.new.jumps("10") 9th September LRUG - @arkh4m

  23. We would get editor.rb:12: Method jumps does not exist on

    Kitten https://srb.help/7003 12 |Kitten.new.jumps("10") ^^^^^^^^^^^^^^^^^^^^^^ Autocorrect: Use `-a` to autocorrect editor.rb:12: Replace with jump 12 |Kitten.new.jumps("10") ^^^^^ Errors: 1 9th September LRUG - @arkh4m 23
  24. How can I use Sorbet? 1. srb command line tool.

    Analyzes the code statically to report potential mistakes in the code. 2. sorbet-runtime gem. This gem will add methods that will also check the code dynamically while it runs. 9th September LRUG - @arkh4m 24
  25. One magic comment to rule them all 9th September LRUG

    - @arkh4m 25
  26. # typed: ignore The file is not even read by

    sorbet. 9th September LRUG - @arkh4m 26
  27. # typed: false Errors related to syntax, constant resolution and

    correctness of signatures. 9th September LRUG - @arkh4m 27
  28. # typed: true Calling a non-existant method, calling a method

    with mismatched arguments, using variables which are inconsistent with their types. 9th September LRUG - @arkh4m 28
  29. # typed: strict All methods must have signatures and all

    variables must have explicitly annotated types. 9th September LRUG - @arkh4m 29
  30. # typed: strong Values cannot be markes as untyped, which

    means the file is 100% statically typed. 9th September LRUG - @arkh4m 30
  31. DEMO TIME! 9th September LRUG - @arkh4m 31

  32. Thank you! Some interesting reads — What To Know Before

    Debating Type Systems — Our journey to type checking 4 million lines of Python — Type Checking in Ruby — Check Yo Self Before You Wreck Yo Self — A plan towards Ruby 3 Types, Yusuke Endoh 9th September LRUG - @arkh4m 32