Upgrade to Pro — share decks privately, control downloads, hide ads and more …

簡潔優雅的語言 —— Crystal

簡潔優雅的語言 —— Crystal

David Kuo (Davy)

February 27, 2016
Tweet

More Decks by David Kuo (Davy)

Other Decks in Technology

Transcript

  1. 8IZ • We love Ruby's efficiency for writing code. •

    We love C's efficiency for running code. • We want the best of both worlds. • We want the compiler to understand what we mean without having to specify types everywhere. • We want full OOP. • Oh, and we don't want to write C code to make the code run faster. IUUQTHJUIVCDPNDSZTUBMMBOHDSZTUBM
  2. (PBMT • Same syntax as Ruby, or at least as

    similar as possible. • Statically type-checked but without having to specify the type of variables or method arguments. • Be able to call C code by writing bindings to it in Crystal. • Have compile-time evaluation and generation of code, to avoid boilerplate code. • Compile to efficient native code. IUUQTHJUIVCDPNDSZTUBMMBOHDSZTUBM
  3. -JLFUIJT # 矦僒 HTTP ⠿剪㐼 require "http/server" server = HTTP::Server.new(8080)

    do |request| HTTP::Response.ok "text/plain", "Hello world, got #{request.path}!" end puts "Listening on http://0.0.0.0:8080" server.listen
  4. -JLFUIJT # 矦僒 HTTP ⠿剪㐼 require "http/server" server = HTTP::Server.new(8080)

    do |request| HTTP::Response.ok "text/plain", "Hello world, got #{request.path}!" end puts "Listening on http://0.0.0.0:8080" server.listen # Ruby 괐呔鏽鍑 䒸Ⰵⴅ䒭䏨 䒊用 Server 暟⟝ 㼩倴䨾剤锞宠㔐⫄ HTTP 200 ⟃⿻Ⰹ㺂 곏爚鎝䜂 Ꟛ㉖ Server
  5. 㘗ⴽ䲀㼬⿻㘗ⴽ增叆 array = [1, 2, 3, 4, 5] array <<

    "string" Error in ./array.cr:2: no overload matches 'Array(Int32)#<<' with types String Overloads are: - Array(Int32)#<<(value : Int32) array << "string” ^~
  6. 㘗ⴽ䲀㼬⿻㘗ⴽ增叆 array = Array.new(Int32 | String) array << 13 array

    << "string" array = [] of Int32 | String array << 13 array << "string"
  7. 㘗ⴽ䲀㼬⿻㘗ⴽ增叆 $ crystal tool context \ --cursor array.cr:3:1 array.cr 1

    possible context found | Expr | Type | --------------------------------- | array | Array(String | Int32) |
  8. 㘗ⴽ䲀㼬⿻㘗ⴽ增叆 array = [1, "str"] $ crystal tool context \

    --cursor array.cr:2:1 array.cr 1 possible context found | Expr | Type | --------------------------------------------------- | array | Array(String | Int32) | | ::Array(typeof(1, "str")).build(2) do |__temp_111| __temp_111[0] = 1 __temp_111[1] = "str" 2 end | Array(String | Int32) |
  9. $#JOEJOH @[Link("cos")] # Link flag lib C # In C:

    double cos(double x) fun cos(value : Float64) : Float64 end C.cos(1.5) #=> 0.0707372
  10. $#JOEJOH @[Link("cos")] # Link flag lib C # In C:

    double cos(double x) fun cos(value : Float64) : Float64 end C.cos(1.5) #=> 0.0707372 https://github.com/manastech/crystal_lib
  11. .BDSP # Macro 爚眕 macro define_method(name, content) def {{name}} {{content}}

    end end define_method foo, 1 foo # Macro 爚眕 def foo 1 end define_method foo, 1 foo #=> 1
  12. /BUJWF$PEF $ crystal build http_server.cr $ ./http_server.cr Listening on http://0.0.0.0:8080

    $ crystal build --release http_server.cr $ ./http_server.cr Listening on http://0.0.0.0:8080
  13. 湡⵹鹎䏞 ˙ 湡⵹晝劥0.12.0䎙⛖嫦Ⰽヰ3FMFBTF♧⦐晝劥 ˙ 湡⵹➠捀 "MQIBꥣ媯 ˋˋ 铃鎊垦彋⿻ TUEMJC 㼿㖈锅侮⚥

    ˙ 䊺㸤䧭荈莊 ˋˋ 植㖈〳⟃欽 $SZTUBM管陼 $SZTUBM管陼㐼✫ ˙ 湡⵹〫佅䴂 -JOVY⿻ .BD049禺窡 ˋˋ 'SFF#4%䊺㸤䧭⡎劢ざ⢘荛♳康
  14. 湡⵹鹎䏞 ˙ 湡⵹晝劥0.12.0䎙⛖嫦Ⰽヰ3FMFBTF♧⦐晝劥 ˙ 湡⵹➠捀 "MQIBꥣ媯 ˋˋ 铃鎊垦彋⿻ TUEMJC 㼿㖈锅侮⚥

    ˙ 䊺㸤䧭荈莊 ˋˋ 植㖈〳⟃欽 $SZTUBM管陼 $SZTUBM管陼㐼✫ ˙ 湡⵹〫佅䴂 -JOVY⿻ .BD049禺窡 ˋˋ 'SFF#4%䊺㸤䧭⡎劢ざ⢘荛♳康 ˋˋ 8JOEPXT䧮鼩㖈⸗⸂⚥˘˘
  15. 4IBSET%FQFOEFODZNBOBHFS name: shards version: 1.2.3 dependencies: openssl: github: datanoise/openssl.cr branch:

    master development_dependencies: minitest: git: https://github.com/ysbaddaden/minitest.cr.git version: "~> 0.1.0" scripts: postinstall: make ext license: MIT
  16. ˙ (PPHMF(SPVQ ˙ IUUQTHSPVQTHPPHMFDPNGPSVNGPSVNDSZTUBMMBOH ˙ *3$$IBOOFM ˙ DSZTUBMMBOH !JSDGSFFOPEFOFU ˙

    $SZTUBM+BQBO6TFST(SPVQ ˙ IUUQTHJUIVCDPNDSZTUBMKQ ˙ IUUQKBDSZTUBMMBOHPSH 爢纈须彂
  17. ˙ (PPHMF(SPVQ ˙ IUUQTHSPVQTHPPHMFDPNGPSVNGPSVNDSZTUBMMBOH ˙ *3$$IBOOFM ˙ DSZTUBMMBOH !JSDGSFFOPEFOFU ˙

    $SZTUBM+BQBO6TFST(SPVQ ˙ IUUQTHJUIVCDPNDSZTUBMKQ ˙ IUUQKBDSZTUBMMBOHPSH 爢纈须彂