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

Static Type Inferencing ... in Ruby?

Static Type Inferencing ... in Ruby?

And exploration of the possibility of doing static type inferencing in Ruby.

Avatar for Danielle Smith

Danielle Smith

February 11, 2019
Tweet

More Decks by Danielle Smith

Other Decks in Programming

Transcript

  1. Static (adj) not able to be changed during a set

    period, for example while a program is running.
  2. Static Type Inferencing to deduce from reasoning, the categories of

    things that are not able to change while a program is running
  3. How to infer type: 1. Parse Code 2. Infer type

    of literals 3. ??? 4. Profit!
  4. Method calls: 1 + 1 Integer has a + method

    that takes an Integer and returns an Integer
  5. foo accepts a and + gets called on a with

    an Integer and it’s the last expr. in the method
  6. foo accepts one arg that has a + method that

    takes an Integer and returns whatever that returns
  7. foo

  8. def foo(a, b=1, c:, **d) x = a > b

    ? c : d “B:#{b + yield(x).upcase}” rescue => e retry end
  9. s(:defn, :foo, s(:args, :a, s(:lasgn, :b, s(:lit, 1)), s(:kwarg, :c),

    :"**d"), s(:rescue, s(:block, s(:lasgn, :x, s(:if, s(:call, s(:lvar, :a), :>, s(:lvar, :b)), s(:lvar, :c), s(:lvar, :d))), s(:dstr, "B:", s(:evstr, s(:call, s(:lvar, :b), :+, s(:call, s(:yield, s(:lvar, :x)), :upcase))))), s(:resbody, s(:array, s(:lasgn, :e, s(:gvar, :$!))), s(:retry))))
  10. Almost impossible to look at a piece of code and

    say definitively what their types are
  11. x = nil if something x = 1 end #

    x is maybe 1 or nil
  12. a = [] # Array[?] a << 1 # Array[Integer]

    a << nil # Array[Int|Nil] a.sample + 1 # undefined method `+’ for symbol