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
670
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
B2B SaaSから見た最近のC#/.NETの進化
sansantech
PRO
0
730
ノーコードデータ分析ツールで体験する時系列データ分析超入門
negi111111
0
410
DMARC 対応の話 - MIXI CTO オフィスアワー #04
bbqallstars
1
160
Terraform Stacks入門 #HashiTalks
msato
0
350
複雑なState管理からの脱却
sansantech
PRO
1
140
BLADE: An Attempt to Automate Penetration Testing Using Autonomous AI Agents
bbrbbq
0
290
マルチプロダクトな開発組織で 「開発生産性」に向き合うために試みたこと / Improving Multi-Product Dev Productivity
sugamasao
1
300
スクラムチームを立ち上げる〜チーム開発で得られたもの・得られなかったもの〜
ohnoeight
2
350
隣接領域をBeyondするFinatextのエンジニア組織設計 / beyond-engineering-areas
stajima
1
270
Lexical Analysis
shigashiyama
1
150
20241120_JAWS_東京_ランチタイムLT#17_AWS認定全冠の先へ
tsumita
2
240
ISUCONに強くなるかもしれない日々の過ごしかた/Findy ISUCON 2024-11-14
fujiwara3
8
870
Featured
See All Featured
10 Git Anti Patterns You Should be Aware of
lemiorhan
654
59k
Building an army of robots
kneath
302
43k
Designing on Purpose - Digital PM Summit 2013
jponch
115
7k
StorybookのUI Testing Handbookを読んだ
zakiyama
27
5.3k
YesSQL, Process and Tooling at Scale
rocio
169
14k
Navigating Team Friction
lara
183
14k
Intergalactic Javascript Robots from Outer Space
tanoku
269
27k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
364
24k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
159
15k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
246
1.3M
Understanding Cognitive Biases in Performance Measurement
bluesmoon
26
1.4k
The Invisible Side of Design
smashingmag
298
50k
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