Slide 1

Slide 1 text

Ruby Programming with Type Checking Soutaro Matsumoto (@soutaro)

Slide 2

Slide 2 text

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)

Slide 3

Slide 3 text

Released 0.3.0, yesterday

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

Demo • Introductory example • Simple Ruby program with two classes

Slide 7

Slide 7 text

Type Checking Steps 1. Write signatures 2. Write Ruby program with annotation 3. Run type checker 4. Goto 1 (2)

Slide 8

Slide 8 text

Demo • Type checking existing Ruby program • 4 years old production Rails application

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

• 1 controller / 2 models • Minimal set of classes/methods Demo Result

Slide 11

Slide 11 text

• 1 controller / 2 models • Minimal set of classes/methods Demo Result

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

• 1 controller / 2 models • Minimal set of classes/methods Demo Result For Other Libraries

Slide 15

Slide 15 text

Only 7 lines of annotations

Slide 16

Slide 16 text

Type Checking Difficulties • Missing library types • Type system expressiveness (params) • Abused metaprogramming (ActiveRecord)

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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?

Slide 20

Slide 20 text

Recap • Try Ruby programming with type checking using Steep
 $ gem install steep • Give me a feedbacks!