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

Ruby 3の新機能としての静的型検査の開発

Ruby 3の新機能としての静的型検査の開発

Soutaro Matsumoto

March 10, 2021
Tweet

More Decks by Soutaro Matsumoto

Other Decks in Programming

Transcript

  1. দຊफଠ࿠ w !TPVUBSP w 3VCZίϛολ ʙ 
 3VCZͷܕͷ࢓ࣄ w 4RVBSFגࣜձࣾ

    ʙ 
 ࣾ಺ͷ3VCZϓϩάϥϜʹܕΛ෇͚Δ࢓ࣄ w ത࢜ʢ޻ֶʣ೥ஜ೾େֶ 
 3VCZϓϩάϥϜͷܕΛਪ࿦
  2. # The conference object represents a conference. # # conf

    = Conference.new("RubyConf 2020") # conf.talks << talk1 # conf.talks << talk2 # class Conference # The name of the conference. attr_reader name: String # Talks of the conference. attr_reader talks: Array[Talk] def initialize: (String name) -> void # Yields all speakers of the conference. # Deduped, and no order guaranteed. def each_speaker: { (Speaker) -> void } -> void | () -> Enumerator[Speaker, void] end
  3. Union types Generics untyped type String | Symbol Integer? #==

    Integer | nil Hash[String, Array[Integer]] Array[String | Integer] def eval: (String) -> untyped Tuple & record [Integer, String] # ݻఆ௕ͷ഑ྻ { id: Integer, name: String } # Ωʔݻఆͷϋογϡ Class/module types Object, Kernel singleton(String), singleton(Array)
  4. class Server < ApplicationRecord validates :name, :length => { :in

    => 3..30 } validates :uri, :length => { :in => 20..200 } validates :ordinal, :numericality => true, :uniqueness => true end class Server < ApplicationRecord attr_accessor name (): String attr_accessor uri (): String def name_changed: () -> bool def name_change: () -> [String?, String?] def name_will_change: () -> void def name_was: () -> String? def name_previously_changed?: () -> bool def name_previously_was: () -> String? def name_before_last_save: () -> String? def uri_changed: () -> bool def uri_change: () -> [String?, String?]
  5. ྺ࢙ w શ෦ܕΛਪ࿦͠Α͏ͱ͢Δάϧʔϓ w ར༻ऀ͕ॻ͍ͨϓϩάϥϜʹܕ஫ऍ͕͋Δ͜ͱΛԾఆ͢Δάϧʔϓ     

     .-ͷܕਪ࿦ʹجͮ͘΋ͷʢদຊ ೆग़ʣ ϑϩʔղੳʹجͮ͘΋ͷʢদຊ ೆग़ʣ %JBNPOECBDL3VCZʢ'VSS 'PTUFSʣ 4UFFQʢদຊʣ 4PSCFU 4USJQF 5ZQF1SPGʢԕ౻ʣ NNDʢࡾӜʣ +VTUJO5JNF4UBUJD5ZQF$IFDLJOHGPS 
 %ZOBNJD-BOHVBHFTʢ3FO 'PTUFSʣ
  6. 3VCZʹ޲͚ͯ     4UFFQʢদຊʣ 4PSCFU 4USJQF 5ZQF1SPGʢԕ౻ʣ NNDʢࡾӜʣ

    3VCZʹܕΛೖΕΔ͜ͱΛએݴ .BU[   3VCZʹܕݕࠪثΛೖΕͳ͍͜ͱΛܾఆ .BU[ 3#4ͷಋೖΛܾఆ .BU[ 5ZQF1SPGͷಋೖΛܾఆ .BU[ ౦๺େֶిؾ௨৴ݚڀॴڞಉϓϩδΣΫτ SVCZUZQFTNFFUJOH
  7. 4PSCFU w IUUQTTPSCFUPSH w 3VCZ%4-ͰܕΛॻ͘ class Conference extend T::Sig sig

    do params(name: String).void end def initialize(name:) @name = name @speakers = T.let([], T::Array[Speaker]) end end
  8. 4UFFQ w ϓϩάϥϜͷܕ͸ผͷఆٛϑΝΠϧʹॻ͘ w ίϝϯτͰΠϯϥΠϯܕ஫ऍ w ࣮ߦ࣌ͷґଘϥΠϒϥϦͳ͠ class Conference def

    each_speaker(&block) # @type var speakers: Array[Speak speakers = [] talks.each do |talk| speakers << talk.speaker end speakers.each(&block) end end class Conference def each_speaker: { (Speaker) -> void } -> void end
  9. ΍Ίͨ w ΠϯλϑΣʔεͷΈߏ଄తʹ෦෼ܕΛ൑ఆ w ଞ͸Ϋϥεͷܧঝؔ܎ʹΑͬͯ൑ఆ interface _Appendable def <<: (String)

    -> void end class String def <<: (String) -> String end class Array[Elem] def <<: (Elem) -> Array[Elem] end class Integer def <<: (Integer) -> Integer end String <: _Appendable # => ✅ Array[String] <: _Appendable # => ✅ Integer <: _Appendable # => 💣
  10. ࣗ༝ͳΦʔόʔϥΠυ w αϒΫϥε͕উखʹ޷͖ͳҙຯɾܕͰಉ໊ͷϝιουΛఆ͍ٛͯ͠Δ w 0CKFDUTFMFDUͱ"SSBZTFMFDU w 0CKFDUNFUIPEͱ)5513FRVFTUNFUIPE w .PEVMFDMBTT def

    select: (Array[IO], Array[IO], Array[IO]) -> Array[Array[IO]] def select: () { (Elem) -> top } -> Array[Elem] | () -> Enumerator[Elem, Array[Elem]] 0CKFDUTFMFDU "SSBZTFMFDU
  11. $PWBSJBOUͳίϨΫγϣϯ w "SSBZ<4USJOH>ͱ"SSBZ<0CKFDU>ͷؔ܎ # @type var strings: Array[String] strings =

    ["foo", "bar"] # @type var objects: Array[Object] objects = strings # ͜ͷ୅ೖΛڐ͔͢ʁ objects.push(3) strings[2] # StringʁIntegerʁ
  12. class Array[Elem] def find: (Elem) -> Elem? end class Array[Elem]

    def find: (top) -> Elem? end # ൺֱؔ਺Λ֎͔Β౉͢ίϨΫγϣϯͩͱμϝͳͷͰ͋Μ·Γ࢖͍ಓ͕ͳͦ͞͏ class Set[Elem] def initialize: { (Elem, Elem) -> Integer } -> void def find: (Elem) -> Elem? end
  13. ิ׬ީิͷੜ੒ foo.ba← ͜͜ʂ foo.← ͜͜ʂ w ͜Ε͸ਖ਼͍͠3VCZίʔυ w ܕݕࠪͯ͠ɺΧʔιϧҐஔͷࣜͷܕ ͔Βิ׬ީิΛੜ੒

    w ͜Ε͸ߏจΤϥʔ w Χʔιϧͷલ͕ͩͬͨΒɺҰจࣈ ফͯ͠Έͯɺߏจղੳ w ղੳͰ͖ͨΒͦͷܕ৘ใ͔Βิ׬ w ҙ֎ͱྑ͍ײ͡Ͱಈ͘
  14. ީิੜ੒଎౓ͷ޲্ w ίʔυ͕௕͍ʢߦͱ͔ʣͱܕݕ͕ࠪ஗͍ʢ໨ඪ͸ඵʣ 
 㱺ؔ܎ͳ͍෦෼Λফ͔ͯ͠Βܕݕࠪ͢Δ class Foo include Bar def

    foo f # ͜͜Ͱิ׬ end def bar ... end w 4UFFQͷܕݕࠪʹ͸࠷΋಺ଆͷEFG Ҏ֎͸ؔ܎ͳ͍ w ϝιουͷܕ͸3#4͔ΒಡΉ w Ωϟογϡͷঢ়گʹ΋ΑΔ͕ɺ࠷ ѱͰඵ͘Β͍
  15. w ଟ૬Ϩίʔυܕʹجͮ͘3VCZϓϩάϥϜͷܕਪ࿦ দຊೆग़  w 3VCZϓϩάϥϜͷ੍ޚϑϩʔղੳͱͦͷ݈શੑͷূ໌ দຊೆग़  w 4UBUJD5ZQF*OGFSFODFGPS3VCZ

    'VSS"O'PTUFS)JDLT  w +VTUJO5JNF4UBUJD5ZQF$IFDLJOHGPS%ZOBNJD-BOHVBHFT 3FO'PTUFS  w 1SPHSFTTSFQPSU3VCZʹ͓͚Δ੩తܕղੳͷ࣮ݱʹ޲͚ͯ ԕ౻দຊ্໺ॅҪদຊ  w 4PSCFUIUUQTTPSCFUPSH w 4UFFQIUUQTHJUIVCDPNTPVUBSPTUFFQ w 3#4IUUQTHJUIVCDPNSVCZSCT w 5ZQF1SPGIUUQTHJUIVCDPNNBNFUZQFQSPG w HFN@SCT@DPMMFDUJPOIUUQTHJUIVCDPNSVCZHFN@SCT@DPMMFDUJPO