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

Crystal introduction

Crystal introduction

at 2015/07/31 crystal meetup #1

2e7087b86608d4497c209eb9ba14d8f5?s=128

Sho Kusano

July 31, 2015
Tweet

Transcript

  1. CRYSTAL A Chimerical Language https://flic.kr/p/a1CnxH

  2. $ whoami Sho Kusano / rosylilly CTO, Magnet Inc docrystal.org

    Author
  3. Prelude Read The F**king Manuals > http:/ /crystal-lang.org/docs/ > http:/

    /crystal-lang.org/api/
  4. Subjects of this slide A.K.A. AGENDA OVERLOADING REFERENCE / VALUE

    MACRO
  5. LET’S GET STARTED OVERLOADING https://flic.kr/p/o82FWy

  6. Hello, World puts "Hello, World" LET’S GET STARTED OVERLOADING

  7. Hello, World # src/kernel.cr:67 def puts(*objects) STDOUT.puts *objects end LET’S

    GET STARTED OVERLOADING
  8. Hello, World module IO # src/io.cr:218 def puts(*objects : _)

    objects.each do |obj| puts obj end nil end end LET’S GET STARTED OVERLOADING
  9. Hello, World module IO # src/io.cr:209 def puts(obj) self <<

    obj puts end end LET’S GET STARTED OVERLOADING
  10. Hello, World module IO # src/io.cr:214 def puts write_byte '\n'.ord.to_u8

    end end LET’S GET STARTED OVERLOADING
  11. Hello, World module IO # src/io.cr:209 def puts(obj) self <<

    obj puts end end LET’S GET STARTED OVERLOADING
  12. Hello, World module IO # src/io.cr:184 def <<(obj) obj.to_s self

    self end end LET’S GET STARTED OVERLOADING
  13. Overload with #to_s class String # src/string.cr:2262 def to_s self

    end # src/string.cr:2266 def to_s(io) io.write Slice.new(cstr, bytesize) end end LET’S GET STARTED OVERLOADING
  14. REFERENCE VS VALUE https://flic.kr/p/mdcba

  15. How to Make REFERENCE VS VALUE class RPoint property :x,

    :y end struct VPoint property :x, :y end Reference Value
  16. “ Differences REFERENCE VS VALUE The differences between a struct

    and a class are: •Invoking new on a struct allocates it on the stack instead of the heap •A struct is passed by value while a class is passed by reference •A struct implicitly inherits from Struct, which inherits from Value. A class implicitly inherits from Reference. http:/ /crystal-lang.org/docs/syntax_and_semantics/structs.html
  17. Let’s write codes REFERENCE VS VALUE def show(point) puts "1:

    #{point.x}x#{point.y}" point.x += 10 puts "2: #{point.x}x#{point.y}" end a = RPoint.new(10, 10) b = VPoint.new(10, 10) puts "Reference" show(a) show(a) puts "Value" show(b) show(b)
  18. Let’s run a code REFERENCE VS VALUE Reference 1: 10x10

    2: 20x10 1: 20x10 2: 30x10 Value 1: 10x10 2: 20x10 1: 10x10 2: 20x10
  19. MAGICAL https://flic.kr/p/aZYKCH MACRO

  20. “ What’s a macro MAGICAL MACRO Macros are methods that

    receive AST nodes at compile-time and produce code that is pasted into a program. http:/ /crystal-lang.org/docs/syntax_and_semantics/macros.html
  21. Usecase in REAL WORLD MAGICAL MACRO # crustache/spec/mustache_spec.cr require "./spec_helper"

    # for ~lambda.json test $calls = 0 {% for name in %w(interpolation sections inverted delimiters comments partials ~lambdas) %} {{ run “./generate_spec_from_json", “#{name.id}.json" }} {% end %}
  22. Usecase in REAL WORLD MAGICAL MACRO # power_assert.cr/src/power_assert.cr macro assert(expression,

    file = __FILE__, line = __LINE__) %result = {{ expression }} unless %result %ast = get_ast({{ expression }}) %breakdowns = %ast.breakdowns %message = String.build do |io| io << " " * PowerAssert.config.global_indent %ast.to_s(io) io << "\n" %breakdowns.to_s(io) end fail %message, {{ file }}, {{ line }} end %result end
  23. Subjects of this slide A.K.A. AGENDA OVERLOADING REFERENCE / VALUE

    MACRO
  24. with lldb https://flic.kr/p/9cnJET DEBUG

  25. How to debug your codes DEBUG with lldb $ crystal

    build target.cr $ lldb target
  26. Realtime Demo DEBUG with lldb

  27. raise IO::EOFError.new