by Craig Buchek
St. Louis Ruby User Group
March 10, 2014
If you know Ruby, it's easy to get started
Syntax is about 90% the same as Ruby
Semantics are about 80% the same as Ruby
(Differences mainly due to being compiled)
class Foo(T)
property :bar
def initialize(@bar : T)
end
end
foo = Foo.new("string")
puts foo.bar #=> "string"
foo2 = Foo(Int).new(123)
puts foo2.bar #=> 123
No support for dynamic features:
define_method
method_missing
eval
Supported "dynamic" features:
instance_eval
responds_to?
macro
Binaries range from 100 KB to 20 MB
Compiles pretty quickly
Binaries are fast
Compiles and runs quickly
Compiling LLVM bytecode to assembler is multithreaded
Types are inferred in most cases
Eliminates NullPointerException errors
def nil_or_string
rand(2) == 0 ? nil : "string"
end
nil_or_string.capitalize #=> Compile error: undefined method 'capitalize' for Nil
Methods can have variants with different signatures
struct Date
def initialize(year : Int, month : Int, day : Int, @calendar = Date::Calendar.default)
@jdn = @calendar.ymd_to_jdn(year, month, day).to_i64
end
def initialize(jdn : Int, @calendar = Date::Calendar.default)
@jdn = jdn.to_i64
end
end
Empty arrays and hashes have to declare their type
x = [1] # OK
x = [1, 1.5, 'a', "abc"] # OK
x = [] # Syntax Error
x = [] of Int
x = [] of String|Nil|Int32
y = {} of Symbol => String
No support for send
Based on the Ruby we love
Strong OOP
Strong sense of getting things right and no surprises
Easy to interface with C libraries
lib LibReadline("readline")
fun readline(prompt : UInt8*) : UInt8*
end
module Readline
def self.readline(prompt)
line = LibReadline.readline(prompt)
line ? String.new(line) : nil
end
end
C types
lib C
fun atoi(str : UInt8*) : Int32
fun atoll(str : UInt8*) : Int64
fun atof(str : UInt8*) : Float64
fun strtof(str : UInt8*, endp : UInt8**) : Float32
fun sprintf(str : UInt8*, format : UInt8*, ...) : Int32
end
Low-level types
Shorthand syntax for initializer params
class URI
def initialize(@scheme, @host, @port, @path, @query_string)
@uri = "#{@scheme}://#{@host}:#{@port}/#{@path}?#{@query_string}"
end
end
Macros for getter
, setter
, property
, and delegate
class Object
macro self.getter(name)"
def #{name}
@#{name}
end
"end
end
Hash initialization takes an optional key comparator
h = Hash(String, Int).new(nil, Hash::CaseInsensitiveComparator)
h["abc"] = 123
puts h["AbC"] #=> 123
Block shorthand syntax
[1, 20, 300].map &.to_s.length #=> [1, 2, 3]
Null checking
method1.not_nil!.method2
Started in September 2012
Standard library is somewhat minimal
Spec library is minimal
describe
it
should
No database support
No Windows support
No debugger
REPL is a hack
Ported compiler from Ruby to self-hosting
They don't want to document too much
Things are broken sometimes
Lots of changes in the past year
Implicit conversions:
String
to UInt8*
for C functionsNil
to 0
for C functions taking a pointerClass v. Struct
Syntax for complex types
No introspection
I like what I see
I think it's pretty fast
Primary Developers