1 if rand(2) == 1
2 value = 1
3 else
4 value = "hi"
5 end
6
7 puts typeof(value) # (Int32 | String) | (Int32 | String)
8 puts value.class # Int32 | String
9 puts value.inspect # 1 | "hi"
Listing 3: Type inference
30 / 43
Slide 31
Slide 31 text
1 class IsString
2 def string?(value : String)
3 "hey, #{value} is string :)"
4 end
5
6 def string?(value : Number)
7 "man, #{value} is a number :|"
8 end
9
10 def string?(value : Array(String)) : (Int32 | String)
11 if value.all? { |word| word.size >= 3 }
12 value.size
13 else
14 "only short values"
15 end
16 end
17 end
18
19 is = IsString.new
20 puts is.string?("Hello") # hey, Hello is string :)
21 puts is.string?(9000) # man, 9000 is a number :|
22 puts is.string?(["cat","dog"]) # 2
23 puts is.string?(["a"]) # Only short values
Listing 4: Method overloading
31 / 43
Slide 32
Slide 32 text
1 class IsString
2 def string?(value : String) : String
3 "hey, #{value} is string :)"
4 end
5
6 def string?(value : Number) : String
7 "man, #{value} is a number :|"
8 end
9
10 def string?(value : String, other)
11 "One of them"
12 end
13
14 def string?(other, value : String)
15 string?(value, nil)
16 end
17
18 def string?(value)
19 "I don't care"
20 end
21 end
22
23 is = IsString.new
24 puts is.string?("ok", 0) # One of them
25 puts is.string?(0, "ok") # One of them
32 / 43
Slide 33
Slide 33 text
1 class Deck
2 @cards = {} of String => Card
3
4 def <<(card : Card)
5 @cards[card.key] = card
6 end
7
8 def <<(key : String)
9 unless empty?(key)
10 @cards[key] = Card.new(key: card)
11 end
12 end
13
14 def <<(card : Nil); end
15
16 private def empty?(content : String)
17 content == ""
18 end
19
20 private def empty?(content : Card); false; end
21
22 private def empty?(content); true; end
23 end
Listing 6: Deck
33 / 43
Slide 34
Slide 34 text
Optimizations
34 / 43
Slide 35
Slide 35 text
▶ Symbols are optimized at compiling time and are stored in a
table of symbols (as integers)
35 / 43
Slide 36
Slide 36 text
▶ Symbols are optimized at compiling time and are stored in a
table of symbols (as integers)
▶ Enum are stored the same way as Symbols
36 / 43
Slide 37
Slide 37 text
1 def blue?(kind)
2 if kind == :blues
3 true
4 else
5 false
6 end
7 end
8
9 blue?(:blue) # false
10 blue?(:red) # false
Listing 7: It compiles!
37 / 43
Slide 38
Slide 38 text
1 enum Color
2 Red
3 Blue
4 end
5
6 def blue?(kind)
7 if kind == Color::Blues
8 puts "Yes"
9 else
10 puts "No"
11 end
12 end
13
14 blue?(Color::Red)
15 blue?(Color::Blue)
16 # Error in code/enum.cr:15: instantiating 'blue?(Color)'
17 #
18 # blue?(Color::Red)
19 # ^~~~~
20 #
21 # in code/enum.cr:8: undefined constant Color::Blues (did you mean 'Color::Blue'
22 #
23 # if kind == Color::Blues
24 # ^~~~~~~~~~~~
Listing 8: It does not compile! 38 / 43
Slide 39
Slide 39 text
1 enum Color
2 Red
3 Blue
4 end
5
6 Color.names.each do |color|
7 puts " # Color::#{color}"
8 puts typeof(color) # String
9 puts color.class # String
10 puts color.inspect # "Red"
11 color_enum = Color.parse(color)
12 puts typeof(color_enum) # Color
13 end
14
15 Color.each do |color|
16 puts " # Color::#{color}"
17 puts typeof(color) # Color
18 puts color.class # Color
19 puts color.inspect # Blue
20 puts color.is_a?(Color) # true
21 end
Listing 9: Enum - Helper methods
39 / 43
Slide 40
Slide 40 text
1 enum Color
2 Red
3 Blue
4 end
5
6 enum Color2
7 OtherRed
8 Black
9 end
10
11 class IsValid
12 def self.color?(color : Color)
13 true
14 end
15
16 def self.color?(color)
17 false
18 end
19 end
20
21 puts IsValid.color?(Color::Red) # true
22 puts IsValid.color?("abc") # false
23 puts IsValid.color?(Color2::Black) # false
Listing 10:
40 / 43
Slide 41
Slide 41 text
1 enum Color
2 Red
3 Blue
4 end
5
6 class IsValid
7 def self.color?(color : Color::Red)
8 "ok"
9 end
10 end
11
12 puts IsValid.color?(Color::Red)
13 # Error in code/enum-4.cr:22: instantiating 'IsValid:Class#color?(Color)'
14 #
15 # puts IsValid.color?(Color::Red)
16 # ^~~~~~
17 #
18 # in code/enum-4.cr:7: Color::Red is not a type, it's a constant
19 #
20 # def self.color?(color : Color::Red)
21 # ^~~~~~~~~~
Listing 11: It does not compile
41 / 43