Crystal

 · 3 mins

Introduction

Crystal is a new, object-oriented, general-purpose, compiled and statically type-checked language, has Ruby-like syntax and is very fast. Let’s break down what that programmer gibberish means.

Object-oriented

Object-Oriented Programming (OOP) means that a language is based around the idea of objects. An important part of OOP are classes and subclasses - imagine it like a water bottle. The water bottle is the class, and the cap, label and actual water are subclasses.

General-purpose

General-purpose is pretty self-explanatory - such languages are not restricted to a certain purpose.

Compiled

A compiled language means it’s translated directly from source code to machine code by a thing called a compiler and is not executed in an interpreter. Examples of compiled languages are: C, Java, C++, BASIC

Static Type-checking

Static type-checking means that errors are caught early by the compiler instead of failing on runtime.

Syntax

Ruby’s syntax is, for many, extremely simple and intuitive. Crystal aims to prove the association of simple languages with slow speed and inefficiency, and so far - it’s doing well.

What does it look like?

Many wouldn’t be able to tell the difference between it and Ruby, so if you’re a Rubyist, the switch would be easy.

An HTTP server in Crystal looks like this:

require "http/server"

server = HTTP::Server.new do |context|
  context.response.content_type = "text/plain"
  context.response.print "Hello world, got #{context.request.path}!"
end

puts "Listening on http://127.0.0.1:8080"
server.listen(8080)

Yes, without formatting, it’s just 7 lines long. If that’s not simple, I don’t know what is!

Additionally, Crystal can achieve concurrency by using green threads (called fibers) Quoting from their website:

Crystal uses green threads, called fibers, to achieve concurrency. Fibers communicate with each other using channels, as in Go or Clojure, without having to turn to shared memory or locks.

Crystal’s libraries are known as shards (inspired by Ruby’s gems), and are relatively simple to install. Simply add the package to the “shards.yml” file in your project and run shards install.

Conclusion

As much as I praise Crystal, it doesn’t come without its flaws. As a relatively new language, it has a very obscure community. For example, in order to search anything about the language, you have to specify Crystal lang, as otherwise something about actual crystals or Crystal Reports would pop up. It’s also not possible to develop it on Windows (outside of VMs or Windows Subsystem for Linux).

There is a scarce amount of libraries due to its novelty, but you can sometimes rewrite an existing Ruby one. That, however, does not stop people like you from supporting it and boosting its popularity. At the end of the day, it’s your choice. Crystal may be simple and fast, but it also has its flaws.

PS: Here’s a handy, basic thing you may miss while learning Crystal: use crystal build filename.cr to compile your code to an executable binary.

Resources