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
OCI Network Firewall 概要
oracle4engineer
PRO
0
4.1k
ハイパーパラメータチューニングって何をしているの
toridori_dev
0
140
Terraform CI/CD パイプラインにおける AWS CodeCommit の代替手段
hiyanger
1
240
AWS Media Services 最新サービスアップデート 2024
eijikominami
0
190
Lexical Analysis
shigashiyama
1
150
Why App Signing Matters for Your Android Apps - Android Bangkok Conference 2024
akexorcist
0
120
Amazon CloudWatch Network Monitor のススメ
yuki_ink
1
200
これまでの計測・開発・デプロイ方法全部見せます! / Findy ISUCON 2024-11-14
tohutohu
3
370
[FOSS4G 2024 Japan LT] LLMを使ってGISデータ解析を自動化したい!
nssv
1
210
誰も全体を知らない ~ ロールの垣根を超えて引き上げる開発生産性 / Boosting Development Productivity Across Roles
kakehashi
1
220
Exadata Database Service on Dedicated Infrastructure(ExaDB-D) UI スクリーン・キャプチャ集
oracle4engineer
PRO
2
3.2k
10XにおけるData Contractの導入について: Data Contract事例共有会
10xinc
5
590
Featured
See All Featured
Code Reviewing Like a Champion
maltzj
520
39k
Documentation Writing (for coders)
carmenintech
65
4.4k
Bash Introduction
62gerente
608
210k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
109
49k
The Illustrated Children's Guide to Kubernetes
chrisshort
48
48k
A Modern Web Designer's Workflow
chriscoyier
693
190k
Designing for humans not robots
tammielis
250
25k
Put a Button on it: Removing Barriers to Going Fast.
kastner
59
3.5k
Six Lessons from altMBA
skipperchong
27
3.5k
Ruby is Unlike a Banana
tanoku
97
11k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
93
16k
A Philosophy of Restraint
colly
203
16k
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