Monday 12 December 2016

Introducing the Crystal Programming Language

It's a dream come true!

Wouldn't it be great if interpreted languages, like Ruby, could be compiled? Also, I hear many people complain that Ruby's dynamic typing makes it weak. This is exactly where Crystal literally shines!

What is Crystal?

 

Crystal is a compiled programming language which is syntactically very similar to Ruby. Most Ruby programs would compile fine under Crystal with minor modifications.

The advantages of using Crystal over Ruby are:
  • Crystal is compiled. Compiled code runs much faster than interpreted code. As a result, time-critical programs can be written in a language which is as slick as Ruby.
  • Crystal is statically typed, i.e., all variables have a type. Despite this, Crystal doesn't require you to assign a type to each variable; the process is automatic.
  • Crystal can call C code. In other words, you can embed C inside Crystal.

Why you might wanna use Ruby over Crystal:
  • Crystal is a newbie. Development began in the mid-2011 and was officially released in June 2014. Crystal is still in its alpha stage.
  • Crystal is not 100% compatible with Ruby. Any non-trivial Ruby program would require changes to become Crystal compliant.
  • Poor support for Windows OS. This is the usual issue with almost all open-source projects: they don't give a damn to Windows and Microsoft.

How to use Crystal?

 

Crystal uses The LLVM Compiler Infrastructure for producing native executable files. I will be talking more about LLVM some other day.


Installing Crystal


I mentioned already that Crystal and Windows don't go well together. In this section, I will explain how to install Crystal on different *NIXes. At the end, I will tell you what can be done on Windows.

Clear and concise installation instructions can be found here: https://crystal-lang.org/docs/installation/index.html

Windows


You can try one of the following workarounds on Windows. Neither is guaranteed to work!
  • Install Cygwin and try building Crystal from source.
  • If you just want to try Crystal, you can do it here: play.crystal-lang.org

Using Crystal

 

Crystal can be used from the command-line. For an introduction to command-line for beginners, see Introduction to Command-line For Complete Beginners.

To execute a program immediately without generating an executable file, run this command:

$ crystal run sourcefile.cr

To create an executable program, run the following commands:

$ crystal build sourcefile.cr
$ ./sourcefile

There are other commands as well along with numerous command-line switches. For the complete guide, look here: https://crystal-lang.org/docs/using_the_compiler/index.html

Hello, World!


Open up your favorite text editor (Vim for me) and save the file as hello.cr with the following contents:

puts "Hello, World!"

Run the following commands in the terminal:

$ crystal build hello.cr
$ ./hello

Neat, right?

Let us know what you think about Crystal in the comments. You can also suggest additional features or support, I will forward them to the Crystal development team.

1 comment: