Crystal Language

First Impressions

 

  • Founder & CEO
  • Machine learning/Deep Learning based solutions, Mumbai

What am I upto?

Let's talk about Crystal

What if Ruby could be compiled?

Ruby is fun and practical

Compiled languages are efficient

Type checking is useful

Crystal is Ruby-like but not ruby-compatible

class Hello

  def initialize(name : String)
    @name = name
  end

  def speak!
    puts "Hello #{@name}!"
  end
end

obj = Hello.new("World")
obj.speak!

That's pretty much Ruby!

▶ crystal build hello_world.cr && ./hello_world
Hello World!

Crystal is compiled  (LLVM)

Became self hosted in Nov 2013

 

  • Work began in June 2011
  • First official release June 2014
  • Current version: 0.26.1 (Aug 28)
  • 4  years old, pre-1.0

Crystal has static types & static dispatch

puts "Hello" + 1

In ruby, this is a runtime error

In crystal, this is caught at compile

▶ ruby sample.cr
sample.cr:1: in `+': no implicit conversion of Integer into String (TypeError)
▶ crystal build sample.cr
Error in sample.cr:1: in `+': no overload matches 'String#+' with type Int32

Crystal is similar to ruby

Live code: Playground

With some nice differences

Live Code: Playground

With an elegant type system

Live code: Playground

Metaprogramming via macros

Live code: Playground

Crystal is quite fast!

def fib(n)
  return n if n <= 1
  fib(n-1) + fib(n-2)
end

puts fib(36)
▶ time ruby fib.cr

14930352

ruby fib.cr  2.27s user 0.56s system 96% cpu 2.931 total




▶  crystal build fib.cr --release && time ./fib

14930352

./fib 0.08s user 0.00s system 91% cpu 0.085 total

 

That's 0.08s vs 2.27s on a  micro-benchmark!

Kemal is like sinatra but very efficient

require "kemal"

get "/" do
  "Hello World"
end

Kemal.run
require "sinatra"

get "/" do
  "Hello World"
end

*From the kemal website:  Kemal is 100+x better on micro-benchmarks!

Where should you use crystal today

Good for micro-services, cli

 

Possibly a good way to speed up your ruby gems!

 

sidekiq, kemal, amber, lucky is a solid stack

 

This should be really good for game engines.

*Though it's pre-1.0 so buyer beware!

What I will like to see in the future

Better windows support - Shipping output to windows could become simple!

 

True parallelism - It's planned but wip...

 

Incremental compile - This will be hard...

 

Some large web code bases and experience with them.

Resources

Thanks!

Let's do some crystal!

Crystal Language - First Impressions

By Anshul Khandelwal

Crystal Language - First Impressions

Some initial thoughts around Crystal and Ruby

  • 734