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

Crystal

 Crystal

工讀分享

David Kuo (Davy)

December 08, 2015
Tweet

More Decks by David Kuo (Davy)

Other Decks in Technology

Transcript

  1. “ ” 8): 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.
  2. # 矦僒 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 㥶姽鸏菛
  3. # 矦僒 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
  4. # ꤏ⴪爚眕 array = [1, 2, 3, 4, 5] array

    << "string" 㘗ⴽ䲀㼬⿻㘗ⴽ增叆
  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" 㘗ⴽ䲀㼬⿻㘗ⴽ增叆
  7. # ꤏ⴪爚眕 array = [] of Int32 | String array

    << 13 array << "string" 㘗ⴽ䲀㼬⿻㘗ⴽ增叆
  8. crystal > $ > crystal tool context \ --cursor array.cr:3:1

    array.cr 1 possible context found | Expr | Type | --------------------------------- | array | Array(String | Int32) | 㘗ⴽ䲀㼬⿻㘗ⴽ增叆
  9. crystal > $ > crystal tool context \ --cursor array.cr:3:8

    array.cr 1 possible context found | Expr | Type | --------------------------------------- | array | Array(String | Int32) | | array << 13 | Array(String | Int32) | 㘗ⴽ䲀㼬⿻㘗ⴽ增叆
  10. crystal > $ > 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) | 㘗ⴽ䲀㼬⿻㘗ⴽ增叆
  11. ::Array(typeof(1, "str")).build(2) do |__temp_111| __temp_111[0] = 1 __temp_111[1] = "str"

    2 end #=> Array(String | Int32) 㘗ⴽ䲀㼬⿻㘗ⴽ增叆
  12. #include <stdio.h> #include <stdlib.h> /* export function */ EXPORT double

    cos(double x) { /* return cos(x) */ } $#*/%*/(
  13. # lib 爚眕 lib C # In C: double cos(double

    x) fun cos(value : Float64) : Float64 end C.cos(1.5) #=> 0.0707372 $#*/%*/(
  14. # lib 爚眕 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 $#*/%*/(
  15. # Macro 爚眕 macro define_method(name, content) def {{name}} {{content}} end

    end define_method foo, 1 ."$30 # Macro 爚眕 def foo 1 end define_method foo, 1 foo #=> 1
  16. crystal > $ > crystal http_server.cr Listening on http://0.0.0.0:8080 crystal

    > $ > crystal run http_server.cr Listening on http://0.0.0.0:8080 /"5*7&$0%&
  17. crystal > $ > crystal build http_server.cr crystal > $

    > ./http_server Listening on http://0.0.0.0:8080 /"5*7&$0%&
  18. crystal > $ > crystal build http_server.cr --release crystal >

    $ > ./http_server Listening on http://0.0.0.0:8080 /"5*7&$0%&
  19. Ꟛ涮鹎䏞 ü $SZTUBM湡⵹➠捀 "MQIBꥣ媯 ˋˋ 铃鎊垦彋⿻ TUEMJC 㼿㖈锅侮⚥ ü 䊺㸤䧭荈莊

    ˋˋ 植㖈〳⟃欽 $SZTUBM管陼 $SZTUBM管陼㐼✫ ü湡⵹〫佅䴂-JOVY⿻.BD049禺窡 ˋˋ'SFF#4%䊺㸤䧭⡎劢ざ⢘荛♳康