Hirofumi Wakasugi
October 23, 2015
110

# 静的型付け言語 Crystal

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

October 23, 2015

## Transcript

(@5t111111)

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

we break easy" c = [1, 2, 3] w ม਺Bͷܕ͸*OU w ม਺Cͷܕ͸4USJOH w ม਺Dͷܕ͸"SSBZ *OU ม਺͸ॳΊͯ஋͕୅ೖ͞Εͨͱ͖ʹએݴ͞ΕΔ
7. ### ม਺ͷఆٛ w ม਺Bͷܕ͸*OU w ม਺Cͷܕ͸4USJOH w ม਺Dͷܕ͸"SSBZ *OU ม਺͸ॳΊͯ஋͕୅ೖ͞Εͨͱ͖ʹએݴ͞ΕΔ a

= 3 b = "we are like crystal, we break easy" c = [1, 2, 3] ܕਪ࿦   5ZQF*OGFSFODF
8. ### ม਺΁ͷ࠶୅ೖ ͜Ε͸Ͳ͏ͳΔʁ Τϥʔʹͳͬͯ͠·͏ͷʁ ͢Ͱʹએݴ͞Εͯܕ͕ܾ·͍ͬͯΔม਺ʹ୅ೖ͢Δ a = "we are like crystal,

it is not easy" a = 100
9. ### ม਺΁ͷ࠶୅ೖ ผͷܕΛม਺ʹ୅ೖͯ͠΋Τϥʔʹ͸ͳΒͳ͍ ม਺ͷܕ͸୅ೖ͞ΕΔࣜ ஋ ʹΑͬͯܕਪ࿦͞ΕΔ ͢Ͱʹએݴ͞Εͯܕ͕ܾ·͍ͬͯΔม਺ʹ୅ೖ͢Δ a = "we are

like crystal, it is not easy" # ここで変数 a は String a = 100 # ここで変数 a は Int32
10. ### "SSBZͷܕ ͜Ε͸Ͳ͏ͳΔʁ ͍͚ͦ͏͡Όͳ͍ʁ *OUͷ"SSBZʹ4USJOHܕͷཁૉΛ௥Ճ͢Δ a = [1, 2, 3] a

<< "we are like crystal"
11. ### "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
12. ### ۭͷ"SSBZ ܕΛࢦఆͤͣʹۭͷ"SSBZΛએݴ͢Δ͜ͱ͸Ͱ͖ͳ͍ a = [] # Syntax Error a =

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

= [1, 2, 3, "we are like crystal"] #=> Array(String | Int32) a = [] of String | Int32 #=> Array(String | Int32)

16. ### ϝιουݺͼग़͠ ͋Δܕ Ϋϥε ʹଘࡏ͠ͳ͍ϝιουΛݺͼग़ͨ͠৔߹ a = 3 a.size undefined method

'size' for Int32 a.size ^~~~ ͜Ε͸ ίϯύΠϧΤϥʔ &SSPS
17. ### ϝιουҾ਺ def put_size(x) puts x.size end put_size("crystal") put_size(5) undefined method

'size' for Int32 puts x.size ^~~~ &SSPS Ҿ਺Ͱ౉͞ΕͨΦϒδΣΫτʹϝιου͕ͳ͍
18. ### ܕ੍໿ 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 Ҿ਺ʹܕΞϊςʔγϣϯΛࢦఆ͠ܕΛ੍໿͢Δ

20. ### 3FϝιουҾ਺ def put_size(x) puts x.size end put_size("crystal") put_size([1, 2, 3])

Ҿ਺Ͱ౉͞ΕͨΦϒδΣΫτʹϝιου͕͋Δ ͑ͬʁ
21. ### 3FϝιουҾ਺ def put_size(x) puts x.size end put_size("crystal") put_size([1, 2, 3])

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

Ҿ਺Ͱ౉͞ΕͨΦϒδΣΫτʹϝιου͕͋Δ 7 3 3FTVMU Τϥʔͳ͘ਖ਼ৗऴྃ͢Δ ໌֬ʹܕ੍໿͕͞Ε͍ͯ ͳ͚Ε͹ɺ͋Δܕ͕ݺͼ ग़͞ΕΔϝιουʹԠ౴ ͢Δͱ͖ɺίϯύΠϥͷ ܕνΣοΫ͸௨Δɻ

24. ### 3F3FϝιουҾ਺ def put_size(x) puts x.size end put_size("crystal") put_size(5) undefined method

'size' for Int32 puts x.size ^~~~ &SSPS ܕ੍໿Λ͠ͳͯ͘΋ίϯύΠϧ࣌ʹΤϥʔ͕ัଊ͞ΕΔ ෆඞཁͳܕ੍໿͸ ॊೈੑΛܽ͘
25. ### Πϯελϯεม਺ Πϯελϯεม਺΋୅ೖ͞ΕΔ஋͔Βܕਪ࿦͞ΕΔ class Person getter name def initialize(@name) end end

barney = Person.new "Barney" barney.name #=> "Barney" barney.name.size #=> 6
26. ### barney = Person.new "Barney" barney.name.size one = Person.new 1 3FΠϯελϯεม਺

Πϯελϯεม਺ʹҟͳΔܕΛ༩͑ͯॳظԽ ͳΜ͔͓͔͍͠ͱ͋͜Δʁ
27. ### 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
28. ### JGWBSJT@B ʜ JT@B ͰܕΛνΣοΫ੍͠ݶ͢Δ if a.is_a?(String) # ここでは a が

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

# size に応答「する」ことが保証される else # ここでは a が # size に応答「しない」ことが保証される end ίϯύΠϧ࣌ϦϑϨΫγϣϯʹΑΔϝιουԠ౴ͷอূ
30. ### 3F3FΠϯελϯεม਺ 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 Πϯελϯεม਺ͷ৔߹͸ɺ্هͷΑ͏ʹҰ౓ม਺ʹ୅ೖ͢Δඞཁ͕͋Δ
31. ### BT BTͰܕΛ੍ݶ͢Δ͜ͱ΋Ͱ͖Δ name = barney.name as String name.size #=> 4

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

a << "crystal" #=> ["crystal"] a << nil #=> ["crystal", nil] w 4USJOH ͸ܕ͕/JMܕΛ࣋ͭ͜ͱΛҙຯ͢Δ w 4USJOHc/JM ͱ͍͏6OJPOܕͱಉ༷
33. ### OJMBCMFΠϯελϯεม਺ JOJUJBMJ[FͰॳظԽ͞Εͳ͍Πϯελϯεม਺ class Person getter name property address def initialize(@name)

end end barney = Person.new "Barney" barney.address = "Manchester" barney.address.size
34. ### OJMBCMFΠϯελϯεม਺ 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ܕΛ࣋ͭ
35. ### 3FOJMBCMFΠϯελϯεม਺ Πϯελϯεม਺Λ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
36. ### Πϯελϯεม਺ͷܕࢦఆ Πϯελϯεม਺ΛܕΞϊςʔγϣϯͰࢦఆ͢Δ class Person … @address :: String … end

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

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

ϚΫϩʹ͓͚Δܕ৘ใ΁ͷΞΫηε w UZQFPGɺ0CKFDUUSZͳͲͷϝιου w FUDFUD