SlideShare une entreprise Scribd logo
1  sur  36
Télécharger pour lire hors ligne
Crystal Powered by Rabbit 2.1.6
Crystal
とみたまさひろ
2015-08-23
Crystal Powered by Rabbit 2.1.6
Crystal
http://crystal-lang.org/
Crystal Powered by Rabbit 2.1.6
Ruby風の言語
Crystal Powered by Rabbit 2.1.6
Ruby風?
Crystal Ruby
静的 動的
型あり 型なし
コンパイル言語 スクリプト言語
Crystal Powered by Rabbit 2.1.6
Crystal プログラム
ふつーに Ruby
class Hoge
def foo
p "abc"
end
end
Hoge.new.foo #=> "abc"
Crystal Powered by Rabbit 2.1.6
実行
% ruby hoge.rb
"abc"
% crystal hoge.rb
"abc"
Crystal Powered by Rabbit 2.1.6
ベンチマーク
# フィボナッチ数列のN番目の値
def fib(n)
if n < 2
n
else
fib(n-1) + fib(n-2)
end
end
p fib(35)
Crystal Powered by Rabbit 2.1.6
速い!
% time ruby fib.rb
9227465
real 0m2.275s
user 0m2.268s
sys 0m0.004s
% time crystal fib.rb
9227465
real 0m0.299s
user 0m0.236s
sys 0m0.056s
Crystal Powered by Rabbit 2.1.6
コンパイルして実行ファイルを生成
% crystal build fib.rb --release
% ./fib
9227465
Crystal Powered by Rabbit 2.1.6
さらに速い!
% time crystal fib.rb
9227465
real 0m0.299s
user 0m0.236s
sys 0m0.056s
% time ./fib
9227465
real 0m0.090s
user 0m0.088s
sys 0m0.000s
Crystal Powered by Rabbit 2.1.6
RubyスクリプトをCrystalで実行
するだけで速くなる!?
Crystal Powered by Rabbit 2.1.6
そんなうまい話はない
Crystal Powered by Rabbit 2.1.6
Crystal ≠ Ruby
Crystal Powered by Rabbit 2.1.6
文字と文字列
"A" - 文字列
'A' - 文字
"A"+"B" #=> "AB"
'A'+'B' #=> undefined method '+' for Char
Crystal Powered by Rabbit 2.1.6
多倍長整数がない
整数演算の謎に見える挙動
0x7FFFFFFF.class #=> Int32
0x80000000.class #=> Int64
0x7FFFFFFF+1 #=> -2147483648
0x7FFFFFFFi64+1 #=> 2147483648
256*256*256 #=> 16777216
256**3 #=> 1.67772e+07
256*256*256*256 #=> 0
256**4 #=> 4.29497e+09
Crystal Powered by Rabbit 2.1.6
Crystal は Crystal として使おう
Crystal Powered by Rabbit 2.1.6
拡張子は .rb じゃなくて .cr
Crystal Powered by Rabbit 2.1.6
Crystalの特徴(Rubyと比較して)
Crystal Powered by Rabbit 2.1.6
型
a = [1, 2, 3] #=> Array(Int32)
a.push 4 #=> OK
a.push "a"
#=> no overload matches 'Array(Int32)#push'
# with types String (コンパイル時エラー)
a = [] #=> Syntax error
a = [] of Int32 #=> OK
Crystal Powered by Rabbit 2.1.6
メソッド
def hoge(a, b)
a + b
end
hoge("abc", "xyz") #=> "abcxyz"
hoge(1, 2) #=> 3
hoge("abc", 2) #=> コンパイル時エラー
Crystal Powered by Rabbit 2.1.6
コンパイル時エラー
% crystal build hoge.cr
Error in ./hoge.cr:7: instantiating 'hoge(String, Int32)'
hoge("abc", 2)
^~~~
in ./hoge.cr:2: no overload matches 'String#+' with types Int32
Overloads are:
- String#+(other : self)
- String#+(char : Char)
a + b
^
Crystal Powered by Rabbit 2.1.6
オーバーロード
def hoge(a, b)
a + b
end
def hoge(a : String, b : Int)
a * b
end
hoge("abc", "xyz") #=> "abcxyz"
hoge(1, 2) #=> 3
hoge("abc", 2) #=> "abcabc"
Crystal Powered by Rabbit 2.1.6
変数に型はない
var = 123
var = 'a'
Crystal Powered by Rabbit 2.1.6
複数の型の可能性
var = rand < 0.5 ? 123 : "abc"
var.size # Int にないのでエラー
var + 1 # String にないのでエラー
Crystal Powered by Rabbit 2.1.6
ちゃんと型チェックすればエラーにならない
var = rand < 0.5 ? 123 : "abc"
if var.is_a? Int
var + 1
else
var.size
end
Crystal Powered by Rabbit 2.1.6
静的
Crystal Powered by Rabbit 2.1.6
静的
eval がない
クラス定義/メソッド定義は後勝ち
Crystal Powered by Rabbit 2.1.6
メソッド定義は後勝ち
if rand < 0.5
def hoge # ← rand の値に関係なく無視される
123 #
end #
else
def hoge # ← 常にこっちが有効
"abc" #
end #
end
Crystal Powered by Rabbit 2.1.6
直感に反したり
def hoge
123
end
if false
def hoge
"abc"
end
end
hoge #=> "abc"
Crystal Powered by Rabbit 2.1.6
その他
Crystal Powered by Rabbit 2.1.6
名前付き引数
Rubyより自然かも
def hoge(n=123, s="abc")
p [n, s]
end
hoge #=> [123, "abc"]
hoge(789) #=> [789, "abc"]
hoge(s: "xyz") #=> [123, "xyz"]
Crystal Powered by Rabbit 2.1.6
マクロ
macro define_method(name, content)
def {{name}}
{{content}}
end
end
define_method foo, 123
foo #=> 123
Crystal Powered by Rabbit 2.1.6
プロジェクト雛形作成(Rails風?)
% crystal init app my_project
create my_project/.gitignore
create my_project/LICENSE
create my_project/README.md
create my_project/.travis.yml
create my_project/Projectfile
create my_project/src/my_project.cr
create my_project/src/my_project/version.cr
create my_project/spec/spec_helper.cr
create my_project/spec/my_project_spec.cr
Initialized empty Git repository in /tmp/my_project/.git/
Crystal Powered by Rabbit 2.1.6
ライブラリ依存関係解決(Bundler風)
my_project% cat Projectfile
deps do
github "manastech/crystal-mysql"
end
my_project% crystal deps install
Cloning into '.deps/manastech-crystal-mysql'...
remote: Counting objects: 55, done.
remote: Total 55 (delta 0), reused 0 (delta 0), pack-reused 55
Receiving objects: 100% (55/55), 7.03 KiB | 0 bytes/s, done.
Resolving deltas: 100% (19/19), done.
Checking connectivity... done.
Crystal Powered by Rabbit 2.1.6
ドキュメント作成(YARD風)
my_project% crystal doc
my_project% ls doc
Myproject.html css index.html js list.html main.html
Crystal Powered by Rabbit 2.1.6
テスト(RSpec風)
my_project% crystal spec
.
Finished in 0.38 milliseconds
1 examples, 0 failures, 0 errors, 0 pending

Contenu connexe

Plus de Masahiro Tomita

お前の罪を数えろ
お前の罪を数えろお前の罪を数えろ
お前の罪を数えろMasahiro Tomita
 
本当はこわいMySQLプロトコル
本当はこわいMySQLプロトコル本当はこわいMySQLプロトコル
本当はこわいMySQLプロトコルMasahiro Tomita
 
ネットワークこわい
ネットワークこわいネットワークこわい
ネットワークこわいMasahiro Tomita
 
MySQLの文字コード事情 2017春版
MySQLの文字コード事情 2017春版MySQLの文字コード事情 2017春版
MySQLの文字コード事情 2017春版Masahiro Tomita
 
MySQLの文字コード事情 2017版
MySQLの文字コード事情 2017版MySQLの文字コード事情 2017版
MySQLの文字コード事情 2017版Masahiro Tomita
 
MySQLの文字コード事情
MySQLの文字コード事情MySQLの文字コード事情
MySQLの文字コード事情Masahiro Tomita
 
「理論から学ぶデータベース実践入門」読書会スペシャル
「理論から学ぶデータベース実践入門」読書会スペシャル「理論から学ぶデータベース実践入門」読書会スペシャル
「理論から学ぶデータベース実践入門」読書会スペシャルMasahiro Tomita
 
アジャイルジャパン長野サテライト
アジャイルジャパン長野サテライトアジャイルジャパン長野サテライト
アジャイルジャパン長野サテライトMasahiro Tomita
 
本当はこわいエンコーディングの話
本当はこわいエンコーディングの話本当はこわいエンコーディングの話
本当はこわいエンコーディングの話Masahiro Tomita
 

Plus de Masahiro Tomita (20)

お前の罪を数えろ
お前の罪を数えろお前の罪を数えろ
お前の罪を数えろ
 
Ruby 2.5
Ruby 2.5Ruby 2.5
Ruby 2.5
 
本当はこわいMySQLプロトコル
本当はこわいMySQLプロトコル本当はこわいMySQLプロトコル
本当はこわいMySQLプロトコル
 
ネットワークこわい
ネットワークこわいネットワークこわい
ネットワークこわい
 
CSV
CSVCSV
CSV
 
MySQLの文字コード事情 2017春版
MySQLの文字コード事情 2017春版MySQLの文字コード事情 2017春版
MySQLの文字コード事情 2017春版
 
MySQLの文字コード事情 2017版
MySQLの文字コード事情 2017版MySQLの文字コード事情 2017版
MySQLの文字コード事情 2017版
 
Ruby24
Ruby24Ruby24
Ruby24
 
MySQLの文字コード事情
MySQLの文字コード事情MySQLの文字コード事情
MySQLの文字コード事情
 
進捗と品質
進捗と品質進捗と品質
進捗と品質
 
MySQLを拡張する
MySQLを拡張するMySQLを拡張する
MySQLを拡張する
 
「理論から学ぶデータベース実践入門」読書会スペシャル
「理論から学ぶデータベース実践入門」読書会スペシャル「理論から学ぶデータベース実践入門」読書会スペシャル
「理論から学ぶデータベース実践入門」読書会スペシャル
 
MyNAができるまで
MyNAができるまでMyNAができるまで
MyNAができるまで
 
文字化け
文字化け文字化け
文字化け
 
メールの暗号化
メールの暗号化メールの暗号化
メールの暗号化
 
文字化け
文字化け文字化け
文字化け
 
進捗と品質
進捗と品質進捗と品質
進捗と品質
 
アジャイルジャパン長野サテライト
アジャイルジャパン長野サテライトアジャイルジャパン長野サテライト
アジャイルジャパン長野サテライト
 
🍣=🍺
🍣=🍺🍣=🍺
🍣=🍺
 
本当はこわいエンコーディングの話
本当はこわいエンコーディングの話本当はこわいエンコーディングの話
本当はこわいエンコーディングの話
 

Dernier

Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024The Digital Insurer
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024The Digital Insurer
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Zilliz
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDropbox
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...apidays
 
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelDeepika Singh
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingEdi Saputra
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxRustici Software
 
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...apidays
 
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu SubbuApidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbuapidays
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfOverkill Security
 

Dernier (20)

Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
 
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu SubbuApidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 

Crystal

  • 1. Crystal Powered by Rabbit 2.1.6 Crystal とみたまさひろ 2015-08-23
  • 2. Crystal Powered by Rabbit 2.1.6 Crystal http://crystal-lang.org/
  • 3. Crystal Powered by Rabbit 2.1.6 Ruby風の言語
  • 4. Crystal Powered by Rabbit 2.1.6 Ruby風? Crystal Ruby 静的 動的 型あり 型なし コンパイル言語 スクリプト言語
  • 5. Crystal Powered by Rabbit 2.1.6 Crystal プログラム ふつーに Ruby class Hoge def foo p "abc" end end Hoge.new.foo #=> "abc"
  • 6. Crystal Powered by Rabbit 2.1.6 実行 % ruby hoge.rb "abc" % crystal hoge.rb "abc"
  • 7. Crystal Powered by Rabbit 2.1.6 ベンチマーク # フィボナッチ数列のN番目の値 def fib(n) if n < 2 n else fib(n-1) + fib(n-2) end end p fib(35)
  • 8. Crystal Powered by Rabbit 2.1.6 速い! % time ruby fib.rb 9227465 real 0m2.275s user 0m2.268s sys 0m0.004s % time crystal fib.rb 9227465 real 0m0.299s user 0m0.236s sys 0m0.056s
  • 9. Crystal Powered by Rabbit 2.1.6 コンパイルして実行ファイルを生成 % crystal build fib.rb --release % ./fib 9227465
  • 10. Crystal Powered by Rabbit 2.1.6 さらに速い! % time crystal fib.rb 9227465 real 0m0.299s user 0m0.236s sys 0m0.056s % time ./fib 9227465 real 0m0.090s user 0m0.088s sys 0m0.000s
  • 11. Crystal Powered by Rabbit 2.1.6 RubyスクリプトをCrystalで実行 するだけで速くなる!?
  • 12. Crystal Powered by Rabbit 2.1.6 そんなうまい話はない
  • 13. Crystal Powered by Rabbit 2.1.6 Crystal ≠ Ruby
  • 14. Crystal Powered by Rabbit 2.1.6 文字と文字列 "A" - 文字列 'A' - 文字 "A"+"B" #=> "AB" 'A'+'B' #=> undefined method '+' for Char
  • 15. Crystal Powered by Rabbit 2.1.6 多倍長整数がない 整数演算の謎に見える挙動 0x7FFFFFFF.class #=> Int32 0x80000000.class #=> Int64 0x7FFFFFFF+1 #=> -2147483648 0x7FFFFFFFi64+1 #=> 2147483648 256*256*256 #=> 16777216 256**3 #=> 1.67772e+07 256*256*256*256 #=> 0 256**4 #=> 4.29497e+09
  • 16. Crystal Powered by Rabbit 2.1.6 Crystal は Crystal として使おう
  • 17. Crystal Powered by Rabbit 2.1.6 拡張子は .rb じゃなくて .cr
  • 18. Crystal Powered by Rabbit 2.1.6 Crystalの特徴(Rubyと比較して)
  • 19. Crystal Powered by Rabbit 2.1.6 型 a = [1, 2, 3] #=> Array(Int32) a.push 4 #=> OK a.push "a" #=> no overload matches 'Array(Int32)#push' # with types String (コンパイル時エラー) a = [] #=> Syntax error a = [] of Int32 #=> OK
  • 20. Crystal Powered by Rabbit 2.1.6 メソッド def hoge(a, b) a + b end hoge("abc", "xyz") #=> "abcxyz" hoge(1, 2) #=> 3 hoge("abc", 2) #=> コンパイル時エラー
  • 21. Crystal Powered by Rabbit 2.1.6 コンパイル時エラー % crystal build hoge.cr Error in ./hoge.cr:7: instantiating 'hoge(String, Int32)' hoge("abc", 2) ^~~~ in ./hoge.cr:2: no overload matches 'String#+' with types Int32 Overloads are: - String#+(other : self) - String#+(char : Char) a + b ^
  • 22. Crystal Powered by Rabbit 2.1.6 オーバーロード def hoge(a, b) a + b end def hoge(a : String, b : Int) a * b end hoge("abc", "xyz") #=> "abcxyz" hoge(1, 2) #=> 3 hoge("abc", 2) #=> "abcabc"
  • 23. Crystal Powered by Rabbit 2.1.6 変数に型はない var = 123 var = 'a'
  • 24. Crystal Powered by Rabbit 2.1.6 複数の型の可能性 var = rand < 0.5 ? 123 : "abc" var.size # Int にないのでエラー var + 1 # String にないのでエラー
  • 25. Crystal Powered by Rabbit 2.1.6 ちゃんと型チェックすればエラーにならない var = rand < 0.5 ? 123 : "abc" if var.is_a? Int var + 1 else var.size end
  • 26. Crystal Powered by Rabbit 2.1.6 静的
  • 27. Crystal Powered by Rabbit 2.1.6 静的 eval がない クラス定義/メソッド定義は後勝ち
  • 28. Crystal Powered by Rabbit 2.1.6 メソッド定義は後勝ち if rand < 0.5 def hoge # ← rand の値に関係なく無視される 123 # end # else def hoge # ← 常にこっちが有効 "abc" # end # end
  • 29. Crystal Powered by Rabbit 2.1.6 直感に反したり def hoge 123 end if false def hoge "abc" end end hoge #=> "abc"
  • 30. Crystal Powered by Rabbit 2.1.6 その他
  • 31. Crystal Powered by Rabbit 2.1.6 名前付き引数 Rubyより自然かも def hoge(n=123, s="abc") p [n, s] end hoge #=> [123, "abc"] hoge(789) #=> [789, "abc"] hoge(s: "xyz") #=> [123, "xyz"]
  • 32. Crystal Powered by Rabbit 2.1.6 マクロ macro define_method(name, content) def {{name}} {{content}} end end define_method foo, 123 foo #=> 123
  • 33. Crystal Powered by Rabbit 2.1.6 プロジェクト雛形作成(Rails風?) % crystal init app my_project create my_project/.gitignore create my_project/LICENSE create my_project/README.md create my_project/.travis.yml create my_project/Projectfile create my_project/src/my_project.cr create my_project/src/my_project/version.cr create my_project/spec/spec_helper.cr create my_project/spec/my_project_spec.cr Initialized empty Git repository in /tmp/my_project/.git/
  • 34. Crystal Powered by Rabbit 2.1.6 ライブラリ依存関係解決(Bundler風) my_project% cat Projectfile deps do github "manastech/crystal-mysql" end my_project% crystal deps install Cloning into '.deps/manastech-crystal-mysql'... remote: Counting objects: 55, done. remote: Total 55 (delta 0), reused 0 (delta 0), pack-reused 55 Receiving objects: 100% (55/55), 7.03 KiB | 0 bytes/s, done. Resolving deltas: 100% (19/19), done. Checking connectivity... done.
  • 35. Crystal Powered by Rabbit 2.1.6 ドキュメント作成(YARD風) my_project% crystal doc my_project% ls doc Myproject.html css index.html js list.html main.html
  • 36. Crystal Powered by Rabbit 2.1.6 テスト(RSpec風) my_project% crystal spec . Finished in 0.38 milliseconds 1 examples, 0 failures, 0 errors, 0 pending