Ruby Programming with Type Checking

Ruby Programming with Type Checking

RubyKaigi 2018

1fab9d01b25e99522f3dfd01e3d4cb51?s=128

Soutaro Matsumoto

June 01, 2018
Tweet

Transcript

  1. 2.

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

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

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

    Type Checking Steps 1. Write signatures 2. Write Ruby program

    with annotation 3. Run type checker 4. Goto 1 (2)
  5. 8.
  6. 9.

    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
  7. 10.
  8. 11.
  9. 12.

    • 1 controller / 2 models • Minimal set of

    classes/methods Demo Result For Models
  10. 13.

    • 1 controller / 2 models • Minimal set of

    classes/methods Demo Result For Rails
  11. 14.

    • 1 controller / 2 models • Minimal set of

    classes/methods Demo Result For Other Libraries
  12. 16.

    Type Checking Difficulties • Missing library types • Type system

    expressiveness (params) • Abused metaprogramming (ActiveRecord)
  13. 17.

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

    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
  15. 19.

    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?
  16. 20.

    Recap • Try Ruby programming with type checking using Steep


    $ gem install steep • Give me a feedbacks!