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
680
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
生成AIをより賢く エンジニアのための RAG入門 - Oracle AI Jam Session #20
kutsushitaneko
4
220
開発生産性向上! 育成を「改善」と捉えるエンジニア育成戦略
shoota
1
270
権威ドキュメントで振り返る2024 #年忘れセキュリティ2024
hirotomotaguchi
2
730
ガバメントクラウドのセキュリティ対策事例について
fujisawaryohei
0
530
Microsoft Azure全冠になってみた ~アレを使い倒した者が試験を制す!?~/Obtained all Microsoft Azure certifications Those who use "that" to the full will win the exam! ?
yuj1osm
2
110
Turing × atmaCup #18 - 1st Place Solution
hakubishin3
0
470
複雑性の高いオブジェクト編集に向き合う: プラガブルなReactフォーム設計
righttouch
PRO
0
110
1等無人航空機操縦士一発試験 合格までの道のり ドローンミートアップ@大阪 2024/12/18
excdinc
0
150
Fanstaの1年を大解剖! 一人SREはどこまでできるのか!?
syossan27
2
160
DevOps視点でAWS re:invent2024の新サービス・アプデを振り返ってみた
oshanqq
0
180
コンテナセキュリティのためのLandlock入門
nullpo_head
2
320
How to be an AWS Community Builder | 君もAWS Community Builderになろう!〜2024 冬 CB募集直前対策編?!〜
coosuke
PRO
2
2.8k
Featured
See All Featured
Docker and Python
trallard
42
3.1k
Unsuck your backbone
ammeep
669
57k
Imperfection Machines: The Place of Print at Facebook
scottboms
266
13k
The Cult of Friendly URLs
andyhume
78
6.1k
How STYLIGHT went responsive
nonsquared
95
5.2k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
45
2.2k
Product Roadmaps are Hard
iamctodd
PRO
49
11k
Designing Experiences People Love
moore
138
23k
GraphQLの誤解/rethinking-graphql
sonatard
67
10k
Building a Modern Day E-commerce SEO Strategy
aleyda
38
7k
Why Our Code Smells
bkeepers
PRO
335
57k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
356
29k
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