Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

“ 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

Slide 17

Slide 17 text

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)

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

“ 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

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

Realtime Demo DEBUG with lldb

Slide 27

Slide 27 text

raise IO::EOFError.new