Slide 1

Slide 1 text

CRYSTAL by Luís Zamith

Slide 2

Slide 2 text

Efficient, Ruby like language FEATURES

Slide 3

Slide 3 text

Static checks FEATURES

Slide 4

Slide 4 text

Macros FEATURES

Slide 5

Slide 5 text

Simple C bindings FEATURES

Slide 6

Slide 6 text

WARNING! Pre-Alpha

Slide 7

Slide 7 text

def fib(n)! if n < 3! 1! else! fib(n-1) + fib(n-2)! end! end! ! fib(35)

Slide 8

Slide 8 text

RUBY def fib(n)! if n < 3! 1! else! fib(n-1) + fib(n-2)! end! end! ! fib(35)

Slide 9

Slide 9 text

CRYSTAL def fib(n)! if n < 3! 1! else! fib(n-1) + fib(n-2)! end! end! ! fib(35)

Slide 10

Slide 10 text

Calculating ------------------------------------- ruby 1.000 i/100ms crystal 2.000 i/100ms ----------------------------------------------------- ruby 1.268 (± 0.0%) i/s - 13.000 crystal 26.321 (± 3.8%) i/s - 264.000 ! Comparison: crystal: 26.3 i/s ruby: 1.3 i/s - 20.75x slower

Slide 11

Slide 11 text

RUBY class Email! attr_reader :subject, :date, :from! ! def initialize(subject, date: "", from: "")! @subject = subject! @date = date! @from = from! end! ! def to_s ! “Date: #{date}! From: #{from}! Subject: #{subject}”! end! end

Slide 12

Slide 12 text

CRYSTAL class Email! getter :subject, :date, :from! ! def initialize(subject, options = {} of Symbol => String)! @subject = subject! @date = options.fetch(:date, "")! @from = options.fetch(:from, "")! end! ! def to_s(io: IO)! io << "Date: #{date}\n! ! ! ! ! ! ! ! From: #{from}\n! Subject: #{subject}\n\n"! end! end

Slide 13

Slide 13 text

CRYSTAL class Email! getter :subject, :date, :from! ! def initialize(subject, options = {} of Symbol => String)! @subject = subject! @date = options.fetch(:date, "")! @from = options.fetch(:from, "")! end! ! def to_s(io: IO)! io << "Date: #{date}\n! ! ! ! ! ! ! ! From: #{from}\n! Subject: #{subject}\n\n"! end! end

Slide 14

Slide 14 text

CRYSTAL class Email! getter :subject, :date, :from! ! def initialize(subject, options = {} of Symbol => String)! @subject = subject! @date = options.fetch(:date, "")! @from = options.fetch(:from, "")! end! ! def to_s(io: IO)! io << "Date: #{date}\n! ! ! ! ! ! ! ! From: #{from}\n! Subject: #{subject}\n\n"! end! end

Slide 15

Slide 15 text

CRYSTAL class Email! getter :subject, :date, :from! ! def initialize(subject, options = {} of Symbol => String)! @subject = subject! @date = options.fetch(:date, "")! @from = options.fetch(:from, "")! end! ! def to_s(io: IO)! io << "Date: #{date}\n! ! ! ! ! ! ! ! From: #{from}\n! Subject: #{subject}\n\n"! end! end

Slide 16

Slide 16 text

Static Checks

Slide 17

Slide 17 text

RUBY def string_size(str)! puts str.length! end! ! string_size("This is a string")! string_size(nil) 16 `string_size': undefined method `length' for nil:NilClass (NoMethodError)

Slide 18

Slide 18 text

CRYSTAL def string_size(str)! puts str.length! end! ! string_size("This is a string")! string_size(nil) instantiating 'string_size(Nil)' undefined method 'length' for Nil

Slide 19

Slide 19 text

Special getters

Slide 20

Slide 20 text

CRYSTAL class Email! getter! :subject, :date, :from! ! ...! end! ! email = Email.new("Homework this week", { date: nil, from: "Ferdous" }),! ! p email.date? # nil

Slide 21

Slide 21 text

CRYSTAL class Email! getter! :subject, :date, :from! ! ...! end! ! email = Email.new("Homework this week", { date: nil, from: "Ferdous" }),! ! p email.date

Slide 22

Slide 22 text

CRYSTAL class Email! getter! :subject, :date, :from! ! ...! end! ! email = Email.new("Homework this week", { date: nil, from: "Ferdous" }),! ! p email.date Nil assertion failed

Slide 23

Slide 23 text

CRYSTAL class Email! getter? :subject, :date, :from! ! ...! end! ! email = Email.new("Homework this week", { date: nil, from: "Ferdous" }),! ! p email.date? # nil

Slide 24

Slide 24 text

CRYSTAL class Email! getter? :subject, :date, :from! ! ...! end! ! email = Email.new("Homework this week", { date: nil, from: "Ferdous" }),! ! p email.date

Slide 25

Slide 25 text

CRYSTAL class Email! getter? :subject, :date, :from! ! ...! end! ! email = Email.new("Homework this week", { date: nil, from: "Ferdous" }),! ! p email.date undefined method 'date' for Email (did you mean 'date?'?)

Slide 26

Slide 26 text

Macros

Slide 27

Slide 27 text

RUBY class BasicObject! def self.attr_reader(*names)! names.map(&:to_s).each do |name|! class_eval "! def #{name}! @#{name}! end! "! end! end! end

Slide 28

Slide 28 text

CRYSTAL class Object! macro getter(*names)! {% for name in names %}! {% name = name.var if name.is_a?(DeclareVar) %}! ! def {{name.id}}! @{{name.id}}! end! {% end %}! end! end

Slide 29

Slide 29 text

C bindings

Slide 30

Slide 30 text

RUBY Not very simple to connect ! Need to write C ! Need to know Ruby’s C implementation (struct, methods, etc…)

Slide 31

Slide 31 text

CRYSTAL lib C! fun atoi(str : UInt8*) : Int32! end! ! p C.atoi(“28").class # Int32

Slide 32

Slide 32 text

CRYSTAL @[Link("readline")]! lib LibReadline! fun readline(prompt : UInt8*) : Pointer(UInt8)! end! ! line = LibReadline.readline("What's your name?\n")! p String.new(line) # What’s your name?! # zamith! # ”zamith"

Slide 33

Slide 33 text

Generics

Slide 34

Slide 34 text

RUBY class Foo! attr_reader :value! ! def initialize(value)! @value = value! end! end! ! foo = Foo.new(1)! p foo.value.abs # 1! ! foo = Foo.new('a')! p foo.value.ord # 97

Slide 35

Slide 35 text

CRYSTAL class Foo! getter :value! ! def initialize(@value); end! end! ! foo = Foo.new(1)! p foo.value.abs # 1! ! foo = Foo.new(‘a’)! p foo.value.ord # undefined method 'abs' for Char

Slide 36

Slide 36 text

CRYSTAL class Foo(T)! getter :value! ! def initialize(@value : T); end! end! ! foo = Foo.new(1)! p foo.value.abs # 1! ! foo = Foo.new('a')! p foo.value.ord # 97

Slide 37

Slide 37 text

CRYSTAL class Array(T)! include Enumerable! include Comparable(Array)! ! def initialize(size, value : T)! ! ! . . .! end! end arr = [1, 2]! arr.first.abs! ! arr = [1, '2']! arr.first.abs # error!

Slide 38

Slide 38 text

Specs

Slide 39

Slide 39 text

CRYSTAL require "spec"! ! class MyString! def initialize(@string); end! ! def size! @string.size! end! end! ! describe "Repo" do! describe "size" do! it "returns the size of my string" do! MyString.new("Hello").size.should eq 5! end! end! end

Slide 40

Slide 40 text

Notable Misses

Slide 41

Slide 41 text

CRYSTAL Single quoted strings ! require_relative ! keyword arguments ! send method_missing ! define_method ! eval

Slide 42

Slide 42 text

Interesting Things

Slide 43

Slide 43 text

CRYSTAL RUBY [1,2].map &.to_s.+(“ hello”) [1,2].map do |num| ! ! num.to_s.+(" hello”)! end # [“1 hello", "2 hello"] # [“1 hello", "2 hello"]

Slide 44

Slide 44 text

CRYSTAL class Hash(K, V)! ...! def self.new(comp = StandardComparator, &block : (Hash(K, V), K -> V))! new block, comp! end! ! def self.new(default_value : V, comp = StandardComparator)! new(comp) { default_value }! end! ! def self.new(comparator)! new nil, comparator! end! ...! end

Slide 45

Slide 45 text

CRYSTAL class Hash(K, V)! ...! def self.new(default_value = nil : V, comp = StandardComparator)! new(comp) { default_value }! end! ...! end

Slide 46

Slide 46 text

CRYSTAL class Hash(K, V)! ...! def self.new(default_value = nil : V?, comp = StandardComparator)! new(comp) { default_value }! end! ...! end

Slide 47

Slide 47 text

CRYSTAL - DEPENDENCIES

Slide 48

Slide 48 text

DEMO