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

静的型付け言語 Crystal

静的型付け言語 Crystal

Tokyo Crystal Meetup #2 (2015-10-23)

Hirofumi Wakasugi

October 23, 2015
Tweet

More Decks by Hirofumi Wakasugi

Other Decks in Programming

Transcript

  1. ม਺ͷఆٛ a = 3 b = "we are like crystal,

    we break easy" c = [1, 2, 3] w ม਺Bͷܕ͸*OU w ม਺Cͷܕ͸4USJOH w ม਺Dͷܕ͸"SSBZ *OU ม਺͸ॳΊͯ஋͕୅ೖ͞Εͨͱ͖ʹએݴ͞ΕΔ
  2. "SSBZͷܕ *OUͷ"SSBZʹ4USJOHܕͷཁૉΛ௥Ճ͢Δ a = [1, 2, 3] a << "we

    are like crystal" no overload matches 'Array(Int32)#<<' with types String Overloads are: - Array(Int32)#<<(value : Int32) a << "we are like crystal" ^~ ͜Ε͸ ίϯύΠϧΤϥʔ &SSPS
  3. ۭͷ"SSBZ ܕΛࢦఆͤͣʹۭͷ"SSBZΛએݴ͢Δ͜ͱ͸Ͱ͖ͳ͍ a = [] # Syntax Error a =

    [] of Int32 # OK! ͱ͍͏͜ͱ͸ɺ ҟͳΔܕͷཁૉΛؚΉ"SSBZ͸ ࡞Εͳ͍ʁ
  4. 6OJPOܕ "SSBZ͸(FOFSJDܕͰ͋Γɺ 6OJPOܕΛؚΉ ཁૉ ͷܕΛࢦఆͰ͖Δ
 )BTIͳͲɺଞͷίϯςφ ίϨΫγϣϯ ܕ΋ಉ༷ ܕͷ૊Έ߹ΘͤΛදݱ͢Δ6OJPOܕ a

    = [1, 2, 3, "we are like crystal"] #=> Array(String | Int32) a = [] of String | Int32 #=> Array(String | Int32)
  5. ϝιουҾ਺ def put_size(x) puts x.size end put_size("crystal") put_size(5) undefined method

    'size' for Int32 puts x.size ^~~~ &SSPS Ҿ਺Ͱ౉͞ΕͨΦϒδΣΫτʹϝιου͕ͳ͍
  6. ܕ੍໿ def put_size(x : String) puts x.size end put_size(5) no

    overload matches 'put_size' with types Int32 Overloads are: - put_size(x : String) put_size(5) ^~~~~~~~ &SSPS Ҿ਺ʹܕΞϊςʔγϣϯΛࢦఆ͠ܕΛ੍໿͢Δ
  7. 3FϝιουҾ਺ def put_size(x) puts x.size end put_size("crystal") put_size([1, 2, 3])

    Ҿ਺Ͱ౉͞ΕͨΦϒδΣΫτʹϝιου͕͋Δ 7 3 3FTVMU Τϥʔͳ͘ਖ਼ৗऴྃ͢Δ
  8. 3FϝιουҾ਺ def put_size(x) puts x.size end put_size("crystal") put_size([1, 2, 3])

    Ҿ਺Ͱ౉͞ΕͨΦϒδΣΫτʹϝιου͕͋Δ 7 3 3FTVMU Τϥʔͳ͘ਖ਼ৗऴྃ͢Δ ໌֬ʹܕ੍໿͕͞Ε͍ͯ ͳ͚Ε͹ɺ͋Δܕ͕ݺͼ ग़͞ΕΔϝιουʹԠ౴ ͢Δͱ͖ɺίϯύΠϥͷ ܕνΣοΫ͸௨Δɻ
  9. 3F3FϝιουҾ਺ def put_size(x) puts x.size end put_size("crystal") put_size(5) undefined method

    'size' for Int32 puts x.size ^~~~ &SSPS ܕ੍໿Λ͠ͳͯ͘΋ίϯύΠϧ࣌ʹΤϥʔ͕ัଊ͞ΕΔ ෆඞཁͳܕ੍໿͸ ॊೈੑΛܽ͘
  10. barney = Person.new "Barney" barney.name.size one = Person.new 1 3FΠϯελϯεม਺

    Πϯελϯεม਺ʹҟͳΔܕΛ༩͑ͯॳظԽ ͳΜ͔͓͔͍͠ͱ͋͜Δʁ
  11. barney = Person.new "Barney" barney.name.size one = Person.new 1 3FΠϯελϯεม਺

    Πϯελϯεม਺ʹҟͳΔܕΛ༩͑ͯॳظԽ undefined method 'size' for Int32 (compile-time type is (String | Int32)) barney.name.size #=> 4 ^~~~ ͜Ε͸ ίϯύΠϧΤϥʔ &SSPS
  12. JGWBSJT@B ʜ JT@B ͰܕΛνΣοΫ੍͠ݶ͢Δ if a.is_a?(String) # ここでは a が

    # String で「ある」ことが保証される else # ここでは a が # String では「ない」ことが保証される end ίϯύΠϧ࣌ϦϑϨΫγϣϯʹΑΔܕͷอূ
  13. JGWBSSFTQPOET@UP ʜ SFTQPOET@UP ͰԠ౴ΛνΣοΫ੍͠ݶ͢Δ if a.responds_to?(:size) # ここでは a が

    # size に応答「する」ことが保証される else # ここでは a が # size に応答「しない」ことが保証される end ίϯύΠϧ࣌ϦϑϨΫγϣϯʹΑΔϝιουԠ౴ͷอূ
  14. 3F3FΠϯελϯεม਺ JT@B ΍SFTQPOET@UP Ͱ੍ݶ͢Δ if (name = barney.name).is_a?(String) name.size #=>

    4 end if (name = barney.name).responds_to?(:size) name.size #=> 4 end Πϯελϯεม਺ͷ৔߹͸ɺ্هͷΑ͏ʹҰ౓ม਺ʹ୅ೖ͢Δඞཁ͕͋Δ
  15. BT BTͰܕΛ੍ݶ͢Δ͜ͱ΋Ͱ͖Δ name = barney.name as String name.size #=> 4

    w BT͸࣮ߦ࣌ʹνΣοΫΛߦ͏ w ର৅͕4USJOHͰͳ͔ͬͨ৔߹ʹ͸ྫ֎͕ൃੜ w BT͸ΩϟετͰ͸ͳ͍ ผͷܕ΁ͷม׵͸͠ͳ͍
  16. OJMBCMF ͋Δܕ͕/JMܕΛ࣋ͭՄೳੑ a = [] of String? a.class #=> Array(String?)

    a << "crystal" #=> ["crystal"] a << nil #=> ["crystal", nil] w 4USJOH ͸ܕ͕/JMܕΛ࣋ͭ͜ͱΛҙຯ͢Δ w 4USJOHc/JM ͱ͍͏6OJPOܕͱಉ༷
  17. OJMBCMFΠϯελϯεม਺ JOJUJBMJ[FͰॳظԽ͞Εͳ͍Πϯελϯεม਺ barney = Person.new "Barney" barney.address = "Manchester" barney.address.size

    undefined method 'size' for Nil (compile-time type is String?) barney.address.size ^~~~ &SSPS ͜Ε͸ ίϯύΠϧΤϥʔ JOJUJBMJ[FͰॳظԽ͞Εͳ͍Πϯελϯεม਺͸/JMܕΛ࣋ͭ
  18. 3FOJMBCMFΠϯελϯεม਺ Πϯελϯεม਺ΛJOJUJBMJ[Fͷ֎ଆͰॳظԽ͢Δ class Person getter name property address @address =

    "nowhere" def initialize(@name) end end barney = Person.new "Barney" barney.address = "Manchester" barney.address.size #=> 10
  19. Πϯελϯεม਺ͷܕࢦఆ Πϯελϯεม਺ΛܕΞϊςʔγϣϯͰࢦఆ͢Δ class Person … @address :: String … end

    w !BEESFTTʹ4USJOHҎ֎ͷܕΛ୅ೖ͢ΔͱΤϥʔ w ܕͷσϑΥϧτ஋͸ଘࡏ͠ͳ͍ͷͰॳظԽ͸ඞཁ
  20. ·ͱΊ w $SZTUBM㱠3VCZ w தͰ΋੩తܕ෇͚ʹΑΔҧ͍͸ඇৗʹେ͖͍ ͦΕͰ΋ɺ We love Ruby's efficiency

    for writing code. ͱ͍͏ͷ͕$SZTUBMͷग़ൃ఺ͷͭͰ͋ΔΑ͏ʹɺ 3VCZͷॊೈੑ΍ੜ࢈ੑɺؾ͍͍࣋ͪॻ͖ຯΛҡ࣋ͯ͠ ੩తܕ෇͚ʹ͠Α͏ͱ͢Δ޻෉͕ਵॴʹݟΒΕΔɻ
  21. ࿩ͤͳ͔ͬͨ࿩୊ͨͪ w (FOFSJDܕ (FOFSJDT  w ྫ֎ॲཧ಺Ͱͷܕਪ࿦ͷಇ͖ w Ϋϩʔδϟ಺Ͱͷม਺ͷܕ w

    ϚΫϩʹ͓͚Δܕ৘ใ΁ͷΞΫηε w UZQFPGɺ0CKFDUUSZͳͲͷϝιου w FUDFUD