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
Tweet

More Decks by Soutaro Matsumoto

Other Decks in Programming

Transcript

  1. Ruby Programming with
    Type Checking
    Soutaro Matsumoto

    (@soutaro)

    View Slide

  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)

    View Slide

  3. Released 0.3.0, yesterday

    View Slide

  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

    View Slide

  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

    View Slide

  6. Demo
    • Introductory example

    • Simple Ruby program with two classes

    View Slide

  7. Type Checking Steps
    1. Write signatures

    2. Write Ruby program with annotation

    3. Run type checker

    4. Goto 1 (2)

    View Slide

  8. Demo
    • Type checking existing Ruby program

    • 4 years old production Rails application

    View Slide

  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

    View Slide

  10. • 1 controller / 2 models

    • Minimal set of classes/methods
    Demo Result

    View Slide

  11. • 1 controller / 2 models

    • Minimal set of classes/methods
    Demo Result

    View Slide

  12. • 1 controller / 2 models

    • Minimal set of classes/methods
    Demo Result
    For Models

    View Slide

  13. • 1 controller / 2 models

    • Minimal set of classes/methods
    Demo Result
    For Rails

    View Slide

  14. • 1 controller / 2 models

    • Minimal set of classes/methods
    Demo Result
    For Other Libraries

    View Slide

  15. Only 7 lines of annotations

    View Slide

  16. Type Checking Difficulties
    • Missing library types

    • Type system expressiveness (params)

    • Abused metaprogramming (ActiveRecord)

    View Slide

  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

    View Slide

  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
    def params: -> { id: String, org_id: String }
    Not Implemented Yet

    View Slide

  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?

    View Slide

  20. Recap
    • Try Ruby programming with type checking using Steep

    $ gem install steep

    • Give me a feedbacks!

    View Slide