The Crystal Programming Language

in #programming6 years ago (edited)

Crystal Logo.png

The most promising programming language during the next couple of years

Since a couple of months now I am observing the development of the Crystal programming language and in this blog post I want to tell you about how I learned about this language and what in particular impresses me about it.

As a longtime Python programmer, self-appointed Pythonista and Python enthusiast as well as evangelist, I was looking for an alternative programming language that was less slow, but at least as mighty and multilayered as Python. This is when I first came across the Crystal programming language, reading about it on Hacker News.

Crystal is a general-purpose and object-oriented programming language that is highly influenced by the Ruby programming language. The inititial designers and developers, Ary Borenszweig and Juan Wajnerman wanted a Ruby like programming language, with the supplement of being a compiled language and optional static type-checking. The authors wanted to create a language 'with the elegance and productivity of Ruby' and the 'efficiency, and type safety of a compiled language'. Sounds awesome, right?

The Syntax

At the first glance Crystal looks exactly the same as Ruby, and in fact a lot of Ruby programs can directly be forwarded to the Crystal compiler without any changes. Consequently for example puts "Hello, World!" works for both worlds. I do not want to investigate every single difference with regard to the syntax, much more I want to get a brief impression on the main differences besides efficiency improvements.

  • No For-Loops.
  • Abstract Classes
  • Static Typing
  • Compile-Time Nil Checks
  • Character Literals

If you want to learn about the differences in more detail, I recommend you the free eBook by Serdar Dogruyol called Crystal For Rubyists.

Efficiency

Now let's take a deeper look on how Crystal compares to Ruby when it comes to efficiency.

Simple Ruby Concurrency
10.times.map do
Thread.new do
puts "What's, up?!"
end
end.each(&:join)


Equivalent Crystal Concurrency
channel = Channel(String).new
10.times do
spawn {
channel.send "Whats, up?!"
}
puts channel.receive
end

A simple benchmark on both programs shows the remarkable difference.

^^/D/Test >>> time ruby foo.rb 20:02:22
What's, up?!
What's, up?!
What's, up?!
What's, up?!
What's, up?!
What's, up?!
What's, up?!
What's, up?!
What's, up?!
What's, up?!
0.43 real 0.08 user 0.02 sys

Whats, up?!
Whats, up?!
Whats, up?!
Whats, up?!
Whats, up?!
Whats, up?!
Whats, up?!
Whats, up?!
Whats, up?!
Whats, up?!
0.02 real 0.00 user 0.00 sys

I know that this is only one specific use case, but nevertheless this shows a speedup of about x21.5(!) and demonstrates what's possibile with Crystal.

Package Management

Whereas Python uses Pip for package management, Crystal utilizes Shards. With Shards you are supposed to list the desired library in a certain YAML file. Running shards install will install each listed dependency automatically.


dependencies:
openssl:
github: datanoise/openssl.cr
branch: master

There are a bunch of websites listing all libraries respectively shards that are currently available in Crystal, see for example CrystalShards.xyz.

Web Development Frameworks

Whereas Python has Django or Flask and Ruby has Ruby on Rails, Crystal has Kemal, which is a web development framework also provided by Serdar Dogruyol.

Final Words

From point of view Crystal is a very neat and promising programming language. Nevertheless it is also a very young language that first appeared around 3 years ago in 2014. Crystal is still not released in a version >=1.0 and its also not yet available for Windows. Both aspects are part of the Crystal team's roadmap and hopefully will be reached during the next couple of months.

Coin Marketplace

STEEM 0.25
TRX 0.11
JST 0.029
BTC 69302.00
ETH 3683.40
USDT 1.00
SBD 3.34