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

Ruby Programming with Type Checking

Ruby Programming with Type Checking

RubyKaigi 2018

Soutaro Matsumoto

June 01, 2018

More Decks by Soutaro Matsumoto

Other Decks in Programming


  1. Steep • https://github.com/soutaro/steep • Gradual typing for Ruby • Type

    annotation as a comment • Type definitions by programmers • Local type inference • Structural subtyping (between object types)
  2. Released 0.3.0, yesterday Automatic implements Type-case on case expression Branch

    specific type annotation Constant type declaration Instance variable type declaration Typing polymorphic methods Revised type inference algorithm nil type Casting to any type masgn support Scaffolding type definitions
  3. Comparison with Sorbet Steep Sorbet Type Inference Local Type Inference

    Type System Generics, union type, any type, context sensitive typing Subtyping Structural Nominal Signature Special Syntax in Another File Ruby code Type Annotation Comment Ruby code Performance Slow Fast
  4. Type Checking Steps 1. Write signatures 2. Write Ruby program

    with annotation 3. Run type checker 4. Goto 1 (2)
  5. Strategy • Even though Rails uses a lot of metaprogramming,

    there is a concrete type representation eventually • Programmers can write down that application's state as type definitions
  6. • 1 controller / 2 models • Minimal set of

    classes/methods Demo Result For Models
  7. • 1 controller / 2 models • Minimal set of

    classes/methods Demo Result For Rails
  8. • 1 controller / 2 models • Minimal set of

    classes/methods Demo Result For Other Libraries
  9. Type Checking Difficulties • Missing library types • Type system

    expressiveness (params) • Abused metaprogramming (ActiveRecord)
  10. Missing Library Types • 40% of the signatures I wrote

    was type of libraries • If you ship your gems with type definitions, we can use that without writing their signatures
  11. Type System Expressiveness • Tuple types and record types can

    improve • Workaround: Use ActionArgs ⚡
 def show: (id: Integer, org_id: Integer) -> void def params: -> Hash<Symbol | String, any> def params: -> { id: String, org_id: String } Not Implemented Yet
  12. Abused Metaprogramming • Steep itself does not understand Rails association

    methods (has_many) or even builtin ones (attr_reader) • Generate signatures by some tools with Rails knowledge • A plugin to generate signature from AR models? • Smarter scaffold command?
  13. Recap • Try Ruby programming with type checking using Steep

    $ gem install steep • Give me a feedbacks!