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

Crystal introduction

Crystal introduction

at 2015/07/31 crystal meetup #1

Sho Kusano

July 31, 2015
Tweet

More Decks by Sho Kusano

Other Decks in Programming

Transcript

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

    View Slide

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

    View Slide

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

    View Slide

  4. Subjects of this slide
    A.K.A. AGENDA
    OVERLOADING
    REFERENCE / VALUE
    MACRO

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  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

    View Slide

  14. REFERENCE
    VS
    VALUE
    https://flic.kr/p/mdcba

    View Slide

  15. How to Make
    REFERENCE VS VALUE
    class RPoint
    property :x, :y
    end
    struct VPoint
    property :x, :y
    end
    Reference Value

    View Slide


  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

    View Slide

  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)

    View Slide

  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

    View Slide

  19. MAGICAL
    https://flic.kr/p/aZYKCH
    MACRO

    View Slide


  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

    View Slide

  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 %}

    View Slide

  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

    View Slide

  23. Subjects of this slide
    A.K.A. AGENDA
    OVERLOADING
    REFERENCE / VALUE
    MACRO

    View Slide

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

    View Slide

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

    View Slide

  26. Realtime Demo
    DEBUG with lldb

    View Slide

  27. raise IO::EOFError.new

    View Slide