Slide 1

Slide 1 text

Dmitry Rocha dev - cm42 Crystal Lang - Intro 1 / 43

Slide 2

Slide 2 text

Dmitry Rocha 2 / 43

Slide 3

Slide 3 text

#soudev 3 / 43

Slide 4

Slide 4 text

codeminer42.com 4 / 43

Slide 5

Slide 5 text

5 / 43

Slide 6

Slide 6 text

▶ Teoria 6 / 43

Slide 7

Slide 7 text

▶ Teoria ▶ Código 7 / 43

Slide 8

Slide 8 text

Teoria 8 / 43

Slide 9

Slide 9 text

”Fast as C, slick as Ruby” crystal-lang.org 9 / 43

Slide 10

Slide 10 text

10 / 43

Slide 11

Slide 11 text

”Fast as C, slick as Ruby” crystal-lang.org 11 / 43

Slide 12

Slide 12 text

nova stdlib ⇐⇒ novas tecnologias 12 / 43

Slide 13

Slide 13 text

13 / 43

Slide 14

Slide 14 text

14 / 43

Slide 15

Slide 15 text

15 / 43

Slide 16

Slide 16 text

16 / 43

Slide 17

Slide 17 text

1.3 self-hosted 17 / 43

Slide 18

Slide 18 text

18 / 43

Slide 19

Slide 19 text

19 / 43

Slide 20

Slide 20 text

20 / 43

Slide 21

Slide 21 text

21 / 43

Slide 22

Slide 22 text

22 / 43

Slide 23

Slide 23 text

Comunidade 23 / 43

Slide 24

Slide 24 text

24 / 43

Slide 25

Slide 25 text

25 / 43

Slide 26

Slide 26 text

26 / 43

Slide 27

Slide 27 text

2. Código 27 / 43

Slide 28

Slide 28 text

1 class Person 2 property age # attr_accessor :age 3 getter name # attr_reader :name 4 5 def initialize(@name : String, @age : Int32) 6 end 7 end Listing 1: Orientação a objetos 28 / 43

Slide 29

Slide 29 text

1 # puts [0,2,3,4].map(&:zero?) 2 puts [0,2,3,4].map(&.==(0)) Listing 2: Map 29 / 43

Slide 30

Slide 30 text

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) : 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 11 "I don't care" 12 end 13 end 14 15 is = IsString.new 16 puts is.string?("Hello") # hey, Hello is string :) 17 puts is.string?(9000) # man, 9000 is a number :| 18 puts is.string?(nil) # I don't care 19 puts is.string?([1,2,3]) # I don't care 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) : String 11 "One of them" 12 end 13 14 def string?(_other, value : String) 15 string?(value, nil) 16 end 17 18 def string?(_value) : String 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 <<(_card : Nil); end 9 10 private def empty?(content : String) : Bool 11 content == "" 12 end 13 14 private def empty?(_content : Card) 15 true 16 end 17 18 private def empty?(_content) 19 false 20 end 21 end Listing 6: Deck 33 / 43

Slide 34

Slide 34 text

Optimizações 34 / 43

Slide 35

Slide 35 text

▶ Símbolos são otimizados em tempo de compilação e ficam numa tabela de símbolos 35 / 43

Slide 36

Slide 36 text

▶ Símbolos são otimizados em tempo de compilação e ficam numa tabela de símbolos ▶ Enum são armazenados do mesmo jeito que símbolos 36 / 43

Slide 37

Slide 37 text

1 def blue?(kind) 2 if kind == :blues # Linha com erro 3 puts "Yes" 4 else 5 puts "No" 6 end 7 end 8 9 blue?(:blue) # => false 10 blue?(:red) # => false Listing 7: Código compila! 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: Código não compila! 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: Métodos auxiliares para Enum 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 IsColor 12 def self.valid?(_color : Color) 13 true 14 end 15 16 def self.valid?(_color) 17 false 18 end 19 end 20 21 puts IsColor.valid?(Color::Red) # => true 22 puts IsColor.valid?("abc") # => false 23 puts IsColor.valid?([123,456]) # => false 24 puts IsColor.valid?(Color2::Black) # => false Listing 10: 40 / 43

Slide 41

Slide 41 text

1 enum Color 2 Red 3 Blue 4 end 5 6 class IsColor 7 def self.valid?(color : Color::Red) 8 "ok" 9 end 10 # Error in code/enum-4.cr:22: instantiating 'IsColor:Class#valid?(Color)' 11 # 12 # puts IsColor.valid?(Color::Red) 13 # ^~~~~~ 14 # 15 # in code/enum-4.cr:7: Color::Red is not a type, it's a constant 16 # 17 # def self.valid?(color : Color::Red) 18 # ^~~~~~~~~~ 19 # 20 end 21 22 puts IsColor.valid?(Color::Red) Listing 11: Código não compila 41 / 43

Slide 42

Slide 42 text

Refs ▶ https://medium.com/@vjdhama/crystal-for-rubyists- e195fcef1614 ▶ https://crystal-lang.org/api/ ▶ https://crystal-lang.org/docs/ ▶ https://github.com/ 42 / 43

Slide 43

Slide 43 text

43 / 43