Crystal
A compiled, Ruby-ish language
Ruby-ISH
COMPILED
class Seasoning
getter :name, :calories
def initialize(@name, @calories=0)
end
SPICY_SEASONINGS = [
"Cayenne",
"Wasabi"
]
def spicy?
SPICY_SEASONINGS.include?(name)
end
end
$ crystal build my_program.cr
color.red = "FF"
^~~
undefined method 'red=' for Color
===================================
$ crystal build my_program.cr
$ ./my_program
"Hello, World!"
It's fast
Type Inference
class Seasoning
getter :name, :calories
def initialize(@name, @calories=0)
end
SPICY_SEASONINGS = [
"Cayenne",
"Wasabi"
]
def spicy?
SPICY_SEASONINGS.include?(name)
end
end
Seasoning.new("Bacon Grease", 50)
Seasoning.new(100) # oops
$ crystal tool hierarchy -e Seasoning seasoning.cr
- class Object (4 bytes)
|
+- class Reference (4 bytes)
|
+- class Seasoning (32 bytes)
@name : (String | Int32) (16 bytes)
@calories : Int32 ( 4 bytes)
Error in ./seasonings.cr:22:
undefined method 'upcase' for Nil
(compile-time type is (Nil | String | Int32))
grease = Seasoning.new("Bacon Grease", 50)
Seasoning.new(nil)
puts grease.name.upcase
Bye Bye...
- Metaprogramming
- define_method, define_const
- method_missing, const_missing
- Class.new
- send(method_name)
- Full-grown language
- gems
- Stack Overflow
- NoMethodError on Nil
Hello...
- Type system
- Macros
- C bindings, nice stdlib, nil-consciousness
- Young language
alias Player = {String, Int32}
alias Team = Array(Players)
# ...
def play_match(home : Team, away : Team); end
def play_match(home : Player, away : Player); end
macro define_method(name, &block)
def {{name.id}}
{{block.body}}
end
end
macro method_missing(name, args, block)
puts "> Called {{name.id}} with {{args.size}} arguments"
end
Try it!
Robert Mosolgo
brew install crystal-lang
crystal-lang.org
#crystal-lang on IRC
Lightning Talk: Crystal
By Robert Mosolgo
Lightning Talk: Crystal
A quick introduction to the Crystal language for RubyConf 2015
- 879