Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Crystal Clean
Search
Luís Ferreira
January 22, 2015
Technology
2
220
Crystal Clean
An overview of the Crystal programming language, with a lot of comparisons with Ruby.
Luís Ferreira
January 22, 2015
Tweet
Share
More Decks by Luís Ferreira
See All by Luís Ferreira
Winter is coming
zamith
0
660
SI - Module5
zamith
0
66
Design Sprints
zamith
0
140
Continuous learning, teaching and the art of improving yourself
zamith
0
130
Clean Code
zamith
3
190
The best language I have ever learned.
zamith
3
1.6k
Testing Magic
zamith
3
1.5k
Clean Code
zamith
7
1.6k
Other Decks in Technology
See All in Technology
pandasはPolarsに性能面で追いつき追い越せるのか
vaaaaanquish
4
4.2k
WINTICKETアプリで実現した高可用性と高速リリースを支えるエコシステム / winticket-eco-system
cyberagentdevelopers
PRO
1
190
AWSコンテナ本出版から3年経った今、もし改めて執筆し直すなら / If I revise our container book
iselegant
15
3.9k
話題のGraphRAG、その可能性と課題を理解する
hide212131
4
1.4k
Emacs x Nostr
hakkadaikon
1
150
マネジメント視点でのre:Invent参加 ~もしCEOがre:Inventに行ったら~
kojiasai
0
430
なんで、私がAWS Heroに!? 〜社外の広い世界に一歩踏み出そう〜
minorun365
PRO
6
1.1k
2024-10-30-reInventStandby_StudyGroup_Intro
shinichirokawano
1
600
Nix入門パラダイム編
asa1984
2
200
Figma Dev Modeで進化するデザインとエンジニアリングの協働 / figma-with-engineering
cyberagentdevelopers
PRO
1
420
とあるユーザー企業におけるリスクベースで考えるセキュリティ業務のお話し
4su_para
3
320
CAMERA-Suite: 広告文生成のための評価スイート / ai-camera-suite
cyberagentdevelopers
PRO
3
260
Featured
See All Featured
What's new in Ruby 2.0
geeforr
342
31k
Keith and Marios Guide to Fast Websites
keithpitt
408
22k
5 minutes of I Can Smell Your CMS
philhawksworth
202
19k
Being A Developer After 40
akosma
86
590k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
42
9.2k
Six Lessons from altMBA
skipperchong
26
3.5k
YesSQL, Process and Tooling at Scale
rocio
167
14k
Fantastic passwords and where to find them - at NoRuKo
philnash
50
2.8k
Become a Pro
speakerdeck
PRO
24
5k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
228
52k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
47
5k
Fashionably flexible responsive web design (full day workshop)
malarkey
404
65k
Transcript
CRYSTAL by Luís Zamith
Efficient, Ruby like language FEATURES
Static checks FEATURES
Macros FEATURES
Simple C bindings FEATURES
WARNING! Pre-Alpha
def fib(n)! if n < 3! 1! else! fib(n-1) +
fib(n-2)! end! end! ! fib(35)
RUBY def fib(n)! if n < 3! 1! else! fib(n-1)
+ fib(n-2)! end! end! ! fib(35)
CRYSTAL def fib(n)! if n < 3! 1! else! fib(n-1)
+ fib(n-2)! end! end! ! fib(35)
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
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
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
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
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
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
Static Checks
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)
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
Special getters
CRYSTAL class Email! getter! :subject, :date, :from! ! ...! end!
! email = Email.new("Homework this week", { date: nil, from: "Ferdous" }),! ! p email.date? # nil
CRYSTAL class Email! getter! :subject, :date, :from! ! ...! end!
! email = Email.new("Homework this week", { date: nil, from: "Ferdous" }),! ! p email.date
CRYSTAL class Email! getter! :subject, :date, :from! ! ...! end!
! email = Email.new("Homework this week", { date: nil, from: "Ferdous" }),! ! p email.date Nil assertion failed
CRYSTAL class Email! getter? :subject, :date, :from! ! ...! end!
! email = Email.new("Homework this week", { date: nil, from: "Ferdous" }),! ! p email.date? # nil
CRYSTAL class Email! getter? :subject, :date, :from! ! ...! end!
! email = Email.new("Homework this week", { date: nil, from: "Ferdous" }),! ! p email.date
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?'?)
Macros
RUBY class BasicObject! def self.attr_reader(*names)! names.map(&:to_s).each do |name|! class_eval "!
def #{name}! @#{name}! end! "! end! end! end
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
C bindings
RUBY Not very simple to connect ! Need to write
C ! Need to know Ruby’s C implementation (struct, methods, etc…)
CRYSTAL lib C! fun atoi(str : UInt8*) : Int32! end!
! p C.atoi(“28").class # Int32
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"
Generics
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
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
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
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!
Specs
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
Notable Misses
CRYSTAL Single quoted strings ! require_relative ! keyword arguments !
send method_missing ! define_method ! eval
Interesting Things
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"]
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
CRYSTAL class Hash(K, V)! ...! def self.new(default_value = nil :
V, comp = StandardComparator)! new(comp) { default_value }! end! ...! end
CRYSTAL class Hash(K, V)! ...! def self.new(default_value = nil :
V?, comp = StandardComparator)! new(comp) { default_value }! end! ...! end
CRYSTAL - DEPENDENCIES
DEMO