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

Introdução a Linguagem Crystal

Introdução a Linguagem Crystal

Apresentação da linguagem Crystal no PUGPI+GURUPI; https://www.youtube.com/watch?v=zckishHu3Es ; e Insiter2017

Dmitry Rocha

April 29, 2017
Tweet

More Decks by Dmitry Rocha

Other Decks in Technology

Transcript

  1. 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
  2. 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
  3. 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
  4. 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
  5. 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
  6. ▶ 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
  7. 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
  8. 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
  9. 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
  10. 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
  11. 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