Exploring Crystal

Scott Ross
CUC4
Published in
3 min readMay 18, 2020
Crystal-Lang

What is Crystal?
Crystal is statically compiled language built on LLVM. It is inspired by Ruby (and the syntax feels like ruby) but is as fast as C, C++ or Rust. It was released in 2014 and joined the TIOBE index in 2016. The goals of the project are pretty straight forward:

  • Syntax similar to Ruby
  • Statically type-checked w/out having to specify type of variables or method arguments
  • Compile to efficient native Code

Why explore Crystal?

I was curious about this language for a couple years, but never thought it was worth diving into. Two things change my mind: First, its a random Sunday night in May and the Coronavirus lock-down is still in place. I spend almost the entirety of my professional career looking at web technologies, and while that’s fun (I guess), this is a nice distraction. The second reason might be more compelling: its an emerging programming language in 2020.

Do I think this will emerge as something you need in your toolkit? … Maybe?

Technical Stuff

Installation.. (I am on an archlinux box .. there’s a homebrew package for this as well)

sudo pacman -S crystal shards## macos
brew update
brew install crystal

Simple example…

Puts "Hello World" # print hello worldclass Greeter
def initialize(@name: String)
end
def sayHello
puts "Hello #{@name}!"
end
end
g = new Greeter.new("World")
puts g.sayHello # Prints Hello World

Feels like ruby. And that’s pretty fantastic.

Longer example:

This code can be compiled with

crystal build cli.cr
# run
./cli -g scott
# Output: Hello Scott!

Package manager — Shards

$ shards init 
$ shards install

Thoughts & musing

First, crystal is not a replacement for Ruby, rust or any other tool you are currently using. Its a maturing language that has hit the hype curve.

It’s probably still near the technology trigger on this graph.

Speaking of Crystal’s fit … and of course, the web … there is a web framework for crystal — Amber. It comes with things you would expect- routing, orm, server side mvc, generators, CLI, and websockets. Conspicuously missing is support for GraphQL (wasn’t obvious). It has 2k star on github.

One of the interesting approaches to Crystal is its approach to its paradigm. It is an OOP language, and imperative. In the world of functional programming, it is an outlier of sorts (one reason I wanted to explore).

There are a bunch of positives for Crystal: 1) extremely low learning curve (similar to ruby), 2) statically typed (right up my alley), 3) extremely fast. Its fit in my professional world would be extremely limited, as its ecosystem still appears to be maturing. But I am excited to test the limits of this language on my personal projects.

--

--