Crystal Lang - Introduction

Crystal Lang - Introduction

Introduction about Crystal Lang for Ruby Nights: The Auckland Ruby Group

https://www.meetup.com/aucklandruby/events/251696482/

63d1307df6bdc998ddc84bf0390c0ff7?s=128

Dmitry Rocha

June 21, 2018
Tweet

Transcript

  1. Dmitry Rocha dev - Qotient Crystal Lang - Intro 1

    / 43
  2. $ whoami Dmitry Rocha 2 / 43

  3. developer 3 / 43

  4. qotient.com 4 / 43

  5. 5 / 43

  6. ▶ Theory 6 / 43

  7. ▶ Theory ▶ Code 7 / 43

  8. Theory 8 / 43

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

  10. 10 / 43

  11. compile, validations, … ”Fast as C, slick as Ruby” crystal-lang.org

    11 / 43
  12. new stdlib ⇐⇒ new technologies 12 / 43

  13. 13 / 43

  14. 14 / 43

  15. 15 / 43

  16. 16 / 43

  17. 1.3 self-hosted 17 / 43

  18. 18 / 43

  19. 19 / 43

  20. 20 / 43

  21. 21 / 43

  22. 22 / 43

  23. Comunity 23 / 43

  24. 24 / 43

  25. 25 / 43

  26. 26 / 43

  27. 2. Code 27 / 43

  28. 1 # Crystal 2 class Person 3 property age :

    Int32 4 getter name 5 6 def initialize(@name : String, born_year : Int32) 7 @age = Time.now.year - born_year 8 end 9 end 10 11 # Ruby 12 require "date" 13 class Person 14 attr_accessor :age 15 attr_reader :name 16 17 def initialize(name, born_year) 18 @name = name 19 @age = Date.today.year - born_year 20 end 21 end 22 23 dude = Person.new("John", 1988) 24 puts dude.age # 29 Listing 1: OO 28 / 43
  29. 1 puts [0,2,3,4].map(&:zero?) # Ruby 2 puts [0,2,3,4].map(&.==(0)) # Crystal

    3 puts [0,2,3,4].map(&.zero?) # Crystal PR#4026 Listing 2: Map 29 / 43
  30. 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
  31. 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
  32. 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
  33. 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
  34. Optimizations 34 / 43

  35. ▶ Symbols are optimized at compiling time and are stored

    in a table of symbols (as integers) 35 / 43
  36. ▶ 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
  37. 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
  38. 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
  39. 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
  40. 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
  41. 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
  42. Refs ▶ https://medium.com/@vjdhama/crystal-for-rubyists- e195fcef1614 ▶ https://crystal-lang.org/api/ ▶ https://crystal-lang.org/docs/ ▶ https://github.com/

    42 / 43
  43. 43 / 43