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

Static typed Ruby starting with YARD

Static typed Ruby starting with YARD

Meguro.rb#16 で「YARD を利用してRubyで静的型付きプログラミングをする話」をしました
cf. https://megurorb.connpass.com/event/90916/

Nao Minami

June 28, 2018
Tweet

More Decks by Nao Minami

Other Decks in Technology

Transcript

  1. ©2018 Wantedly, Inc. 4UFFQͷܕఆٛϑΝΠϧ class Person @name: String @contacts: Array<Email

    | Phone> def initialize: (name: String) -> any def name: -> String def contacts: -> Array<Email | Phone> def guess_country: -> (String | nil) end
  2. ©2018 Wantedly, Inc. 4UFFQͷ1SPT$POT w 1SPT w 3VCZͰ੩తܕݕ͕ࠪग़དྷΔʂιϑτ΢ΣΞ։ൃͷॿ͚ʹͳͬͯ͘ΕΔʂ w $POT

    w ܕఆٛϑΝΠϧͷઐ༻จ๏Λ֮͑Δඞཁ͕͋Δ w ܕఆٛϑΝΠϧΛ͔Βॻ͘ͷ͕େมʂ w ATUFFQTDB⒎PMEAίϚϯυ͕ॿ͚ʹͳͬͯ͘ΕΔ͕ɻɻɻ
  3. ©2018 Wantedly, Inc. 3VCZίʔυΛ༻ҙ͢Δ # lib/parser.rb class AST # @return

    [String] def to_s "AST" end # @param [Hash] opt def traverse!(opt) end end class Parser # @param [String] text # @param [Hash] opt # @return [AST] def parse(text, opt) ast = AST.new ast.traverse!(opt) ast end end
  4. ©2018 Wantedly, Inc. ZBSETUFFQίϚϯυΛ࣮ߦ $ yard2steep lib ZBSETUFFQͰܕఆٛϑΝΠϧΛੜ੒ # sig/parser.rbi

    class AST def to_s: -> String def traverse!: (Hash<any, any>) -> any end class Parser def parse: (String, Hash<any, any>) -> AST end
  5. ©2018 Wantedly, Inc. ܕݕࠪͰࣦഊͤͯ͞ΈΔ # lib/parser.rb class AST # @return

    [String] def to_s "AST" end # @param [Hash] opt def traverse!(opt) end end class Parser # @param [String] text - # @param [Hash] opt + # @param [Hash, nil] opt # @return [AST] - def parse(text, opt) + def parse(text, opt: nil) ast = AST.new ast.traverse!(opt) ast end end
  6. ©2018 Wantedly, Inc. ܕݕࠪͰࣦഊͤͯ͞ΈΔ $ yard2steep lib && steep check

    lib lib/parser.rb:18:18: ArgumentTypeMismatch: receiver=::AST, expected=::Hash<any, any>, actual=(::Hash<any, any> | nil) (opt) ZBSETUFFQͱTUFFQΛ૊Έ߹ΘͤͯܕνΣοΫ ͪΌΜͱɺAOJMAνΣοΫ͍ͯ͠ͳ͍ίʔυͰΤϥʔ ͕ग़ͨʂ
  7. ©2018 Wantedly, Inc. ZBSETUFFQͷࠓޙʹ͍ͭͯ w ΍Γ͍ͨࣄ͸ͨ͘͞Μ w :"3%BOOPUBUJPOͷϦονͳه๏Λαϙʔτ w BUUS@

    ͳͲɺ3VCZͷ૊ΈࠐΈͷNFUBQSPHSBNNJOHܥͷػೳΛTVQQPSU w 3VCZQBSTFSͷվળ ࣗલ࣮૷3JQQFSϕʔε΁Ҡߦ  w FUD w 4UFFQଆʹ΋DPOUSJCVUF͍ͨ͠ w CVJMUJONFUIPEͷܕ෇͚ w PQFODMBTTͷTVQQPSU w CVHpY ػೳ௥Ճ
  8. ©2018 Wantedly, Inc. ͓·͚େมͩͬͨࣄ w 3VCZίʔυͷQBSTFSΛࣗલ࣮૷ w ίϯςΩετΛ೺Ѳ͢ΔͨΊʹFOEΛೝࣝ͢Δඞཁ͕͋ͬͯɺ͔ͳΓେมͩͬͨɻɻɻޙஔJG Ή͔͍ͣ͠ɻɻɻ w

    3JQQFSTFYQͩͱίϝϯτ͕ফ͑ͯ͠·͏ͷͰࣗલʹ͕ͨ͠ɺ3JQQFSϕʔεʹҠߦ༧ఆʂ w 4UFFQͷڍಈ w ADMBTTTFMGA͕TVQQPSU͞Ε͍ͯͳ͔ͬͨΓɺCVJMUJODMBTT΁ͷܕ෇͚͕଍Γͳͯ͘खؒ औͬͨ w AJGdA΍ANBQ TUSJQ A͕͏·͘ಈ͔ͳ͔ͬͨΓɺAEFGOJM  CPPMA͕QBSTFFSSPS ʹͳͬͯۤ͠ΊΒΕͨ