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

Have your Sorbet and eat it too

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

More Decks by Ju Liu

Other Decks in Programming


  1. Hello! ! — My name is Ju — I work

    for NoRedInk — You can find me online as @arkh4m 9th September LRUG - @arkh4m 2
  2. 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
  3. Different types of Type Systems — Dynamic type systems —

    Static type systems 9th September LRUG - @arkh4m 5
  4. 2. Tooling Great refactoring support, since the computer knows more

    about the program. 9th September LRUG - @arkh4m 9
  5. 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
  6. 4. Performance Just like tooling, if the compiler knows more

    about the code, it can optimize it too. 9th September LRUG - @arkh4m 11
  7. NoRedInk — 420'000 lines of Elm in production — 30

    engineers working in the team — Almost no runtime exceptions 9th September LRUG - @arkh4m 13
  8. 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
  9. 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
  10. # 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
  11. If we wrote Kitten.new.jump(7) Sorbet would tell us No errors!

    Great job. 9th September LRUG - @arkh4m 19
  12. 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
  13. 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
  14. 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
  15. # typed: ignore The file is not even read by

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

    correctness of signatures. 9th September LRUG - @arkh4m 27
  17. # 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
  18. # typed: strict All methods must have signatures and all

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

    means the file is 100% statically typed. 9th September LRUG - @arkh4m 30
  20. 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