Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
CRYSTAL - STATICALLY TYPED RUBY
Vagmi Mudumbai - @vagmi (github / twitter)
τarka λabs
τarka λabs@vagmi
puts "hello RubyConfIndia"
τarka λabs@vagmi
Juan WajnermanAry Borenszweig
http://manas.com.ar/
τarka λabs@vagmi
Apache 2.0 License
τarka λabs@vagmi
‣Inspired by Ruby
‣Statically typed
‣Garbage Collected
‣Compiles with LLVM
‣More performant than Ruby
τarka λabs@vagmi
HTTP
JSON
YAML
Fiber
Channel
Spec
OpenSSL
Enumerable
Markdown
OAuth / OAuth2
XML
Web Sockets
τarka λabs@vagmi
τarka λabs@vagmi
⍺
τarka λabs@vagmi
Demo
τarka λabs@vagmi
puts "crystal" + "programming"
puts "1+1 = #{1 + 1}"
puts "7.0/3.0 = #{7.0 / 3.0}"
puts true && false
put...
τarka λabs@vagmi
a = "RubyConf India 2016"
puts a # => "RubyConf India 2016"
puts typeof(a) # => String
τarka λabs@vagmi
a = 42
puts a # => 42
puts typeof(a) # => Int32
τarka λabs@vagmi
y = uninitialized Int32
y = 42
puts y
# y = "@vagmi"
# => Error in ./variables/variables.cr:15:
# type mu...
τarka λabs@vagmi
z = uninitialized (Int32 | String)
puts z # segfault
# Invalid memory access (signal 11) at address 0x7ff...
τarka λabs@vagmi
a = 20
if rand(100) < 50
a = “@vagmi"
end
puts typeof(a) # => (String | Int32)
τarka λabs@vagmi
tuple = {1, 2.5, 'a'}
puts tuple[0] # => 1
puts typeof(tuple) #=> {Int32, Float64, Char}
a = Tuple.new(20...
τarka λabs@vagmi
a = {} of String => Int32
a["year"] = 2016
puts a
b = Hash(Int32 | Char, Int32) {3 => 4}
b[1] = 2
b['a'] ...
τarka λabs@vagmi
b.delete('a')
puts b # => {3 => 4, 1 => 2}
p b[true] # runtime error
# should this not be a compile error...
τarka λabs@vagmi
if(result)
puts result + 2
end
result = b[1]? + 2
# Error in hash.cr:5: undefined method '+' for Nil
# (c...
τarka λabs@vagmi
def greet(name)
"Hello #{name}"
end
puts greet("RubyConf") # => Hello RubyConf
τarka λabs@vagmi
class String
def greet
"Hello #{self}"
end
end
"RubyConf".greet # => Hello RubyConf
τarka λabs@vagmi
def add2(num)
2 + num
end
puts add2(40) # => 42
puts add2("forty")
# in ./def/funs.cr:17: no overload mat...
τarka λabs@vagmi
abstract class Foo
abstract def foo
def greet
"hello #{@name}"
end
end
class Bar < Foo
def initialize(@na...
τarka λabs@vagmi
- class Object (4 bytes)
+- class Reference (4 bytes)
+- class Foo (16 bytes)
. @name : String? (8 bytes)...
τarka λabs@vagmi
struct Entry
property :title, :score, :num_comments
def initialize(@title, @score, @num_comments)
end
def...
τarka λabs@vagmi
- class Object (4 bytes)
+- struct Value (0 bytes)
| +- struct Struct (0 bytes)
| +- struct Entry (16 byt...
τarka λabs@vagmi
macro property(*names)
getter {{*names}}
setter {{*names}}
end
τarka λabs@vagmi
macro getter(*names)
{% for name in names %}
{% if name.is_a?(TypeDeclaration) %}
@{{name.id}}
{% name = ...
τarka λabs@vagmi
macro setter(*names)
{% for name in names %}
{% if name.is_a?(TypeDeclaration) %}
@{{name}}
def {{name.va...
τarka λabs@vagmi
def make_request
url = "https://www.reddit.com/hot.json"
channel = Channel(String).new
spawn do
HTTP::Cli...
τarka λabs@vagmi
get '/feed' do |env|
channel = make_request
entries = JSON.parse(channel.receive)["data"]
["children"].as...
τarka λabs@vagmi
Crystal Language - http://crystal-lang.org/
Support Crystal - https://salt.bountysource.com/teams/crystal...
τarka λabs@vagmi
THANKS
@vagmi / @tarkalabs
github / twitter
Upcoming SlideShare
Loading in …5
×

Crystal - Statically Typed Ruby

1,877 views

Published on

Slides for my talk at RubyConf India 2016. Crystal Language is a statically typed Ruby that allows you to write high performant ruby code.

Published in: Technology
  • Be the first to comment

Crystal - Statically Typed Ruby

  1. 1. CRYSTAL - STATICALLY TYPED RUBY Vagmi Mudumbai - @vagmi (github / twitter) τarka λabs
  2. 2. τarka λabs@vagmi puts "hello RubyConfIndia"
  3. 3. τarka λabs@vagmi Juan WajnermanAry Borenszweig http://manas.com.ar/
  4. 4. τarka λabs@vagmi Apache 2.0 License
  5. 5. τarka λabs@vagmi ‣Inspired by Ruby ‣Statically typed ‣Garbage Collected ‣Compiles with LLVM ‣More performant than Ruby
  6. 6. τarka λabs@vagmi HTTP JSON YAML Fiber Channel Spec OpenSSL Enumerable Markdown OAuth / OAuth2 XML Web Sockets
  7. 7. τarka λabs@vagmi
  8. 8. τarka λabs@vagmi ⍺
  9. 9. τarka λabs@vagmi Demo
  10. 10. τarka λabs@vagmi puts "crystal" + "programming" puts "1+1 = #{1 + 1}" puts "7.0/3.0 = #{7.0 / 3.0}" puts true && false puts true || false puts !true
  11. 11. τarka λabs@vagmi a = "RubyConf India 2016" puts a # => "RubyConf India 2016" puts typeof(a) # => String
  12. 12. τarka λabs@vagmi a = 42 puts a # => 42 puts typeof(a) # => Int32
  13. 13. τarka λabs@vagmi y = uninitialized Int32 y = 42 puts y # y = "@vagmi" # => Error in ./variables/variables.cr:15: # type must be Int32, not (String | Int32)
  14. 14. τarka λabs@vagmi z = uninitialized (Int32 | String) puts z # segfault # Invalid memory access (signal 11) at address 0x7fff571cb200
  15. 15. τarka λabs@vagmi a = 20 if rand(100) < 50 a = “@vagmi" end puts typeof(a) # => (String | Int32)
  16. 16. τarka λabs@vagmi tuple = {1, 2.5, 'a'} puts tuple[0] # => 1 puts typeof(tuple) #=> {Int32, Float64, Char} a = Tuple.new(2016, “rubyconf india", 'x') puts a # => {2016, “rubyconf india", 'x'} puts typeof(a) #=> {Int32, String, Char}
  17. 17. τarka λabs@vagmi a = {} of String => Int32 a["year"] = 2016 puts a b = Hash(Int32 | Char, Int32) {3 => 4} b[1] = 2 b['a'] = 9 puts b # => {3 => 4, 1 => 2, 'a' => 9} puts b.size # => 3
  18. 18. τarka λabs@vagmi b.delete('a') puts b # => {3 => 4, 1 => 2} p b[true] # runtime error # should this not be a compile error? # => Missing hash key: true (KeyError) p b["bar"]? # nil
  19. 19. τarka λabs@vagmi if(result) puts result + 2 end result = b[1]? + 2 # Error in hash.cr:5: undefined method '+' for Nil # (compile-time type is Int32?)
  20. 20. τarka λabs@vagmi def greet(name) "Hello #{name}" end puts greet("RubyConf") # => Hello RubyConf
  21. 21. τarka λabs@vagmi class String def greet "Hello #{self}" end end "RubyConf".greet # => Hello RubyConf
  22. 22. τarka λabs@vagmi def add2(num) 2 + num end puts add2(40) # => 42 puts add2("forty") # in ./def/funs.cr:17: no overload matches # 'Int32#+' with type String def add2(other : String) "#{other} two" end puts add2("forty") # => forty two
  23. 23. τarka λabs@vagmi abstract class Foo abstract def foo def greet "hello #{@name}" end end class Bar < Foo def initialize(@name) end def foo 1 end end a = Bar.new("Bar") puts a.foo # => 1 puts a.greet # => Hello Bar class Baz < Foo def foo 2 end end b = Baz.new puts b.foo # => 2 p b.greet # => "Hello "
  24. 24. τarka λabs@vagmi - class Object (4 bytes) +- class Reference (4 bytes) +- class Foo (16 bytes) . @name : String? (8 bytes) +- class Baz (16 bytes) +- class Bar (16 bytes) crystal tool hierarchy -e Foo abstract/abstract.cr
  25. 25. τarka λabs@vagmi struct Entry property :title, :score, :num_comments def initialize(@title, @score, @num_comments) end def to_json(io) { title: title, score: score, num_comments: num_comments }.to_json(io) end end
  26. 26. τarka λabs@vagmi - class Object (4 bytes) +- struct Value (0 bytes) | +- struct Struct (0 bytes) | +- struct Entry (16 bytes) | | @title : String (8 bytes) | | @score : Int32 (4 bytes) | | @num_comments : Int32 (4 bytes) crystal tool hierarchy -e Entry src/hello-kemal.cr
  27. 27. τarka λabs@vagmi macro property(*names) getter {{*names}} setter {{*names}} end
  28. 28. τarka λabs@vagmi macro getter(*names) {% for name in names %} {% if name.is_a?(TypeDeclaration) %} @{{name.id}} {% name = name.var %} {% end %} def {{name.id}} @{{name.id}} end {% end %} end
  29. 29. τarka λabs@vagmi macro setter(*names) {% for name in names %} {% if name.is_a?(TypeDeclaration) %} @{{name}} def {{name.var.id}}=(@{{name.var.id}} : {{name.type}}) end {% else %} def {{name.id}}=(@{{name.id}}) end {% end %} {% end %} end
  30. 30. τarka λabs@vagmi def make_request url = "https://www.reddit.com/hot.json" channel = Channel(String).new spawn do HTTP::Client.get(url) do |response| if(resp_body = response.body_io.gets) $cache = resp_body channel.send resp_body end end end channel end
  31. 31. τarka λabs@vagmi get '/feed' do |env| channel = make_request entries = JSON.parse(channel.receive)["data"] ["children"].as_a feed = entries.map do |e| Entry.from_json(e) end env.response.content_type = "application/json" feed.to_json end
  32. 32. τarka λabs@vagmi Crystal Language - http://crystal-lang.org/ Support Crystal - https://salt.bountysource.com/teams/crystal-lang Crystal Shards - https://crystalshards.herokuapp.com/ Awesome Crystal - http://awesome-crystal.com/
  33. 33. τarka λabs@vagmi THANKS @vagmi / @tarkalabs github / twitter

×