The Crystal programming language has a syntax that is easy to learn, very much inspired by Ruby, and also has the option of compiling applications directly into machine code. As a general-purpose language, it should be powerful enough for a wide variety of tasks. The own compiler saves optimization measures, such as those necessary with JIT and Co. – even if a Crystal program is to achieve very high performance. Developers who like to learn new programming languages on a regular basis will find Crystal to be an interesting option that is sufficiently different from established languages.
The extensive possibilities of meta-programming also speak in favor of Crystal. In the world of Crystal, this means that code can modify itself or generate itself. Although the flexibility of compiling into native code is not as great as that of languages interpreted at runtime, Crystal is vastly superior to preprocessor solutions that are widely used in C-related languages.
History: Inspired by Ruby, but faster
Deprecations of components in an ecosystem can have dramatic consequences for developers. While the omission of a library is usually easy to cope with, the discontinuation of a programming language in an ongoing software project is tantamount to a total economic loss – the entire code basis becomes unusable, as the “adventure” around Visual Basic 6 clearly showed. For this reason, developers are well advised not to get involved in programming language projects that are too exotic.
The Crystal programming language was launched in 2011 by the Argentine consulting company Manas Technology Solutions. The developers Ary Borenszweig, Juan Wajnerman and Brian Cardiff were initially strongly inspired by Ruby, but then quickly looked for ways to achieve higher performance in the resulting systems.
Almost two years ago, Crystal reached the maturity of a major release 1.0 and is enjoying growing popularity. All around that GitHub-Repository The programming language available as open source under the Apache 2.0 license has created an active ecosystem with more than 500 contributors – and Crystal currently has more than 18,000 GitHub stars in version 1.6.2 (see Fig. 1).
Programs created with Crystal work on the processor architectures x86, x64 and the two ARM variants (32 and 64 bit) under Linux, macOS and FreeBSD. However, support for Windows is still experimental at the time of writing this article.
Installation von Crystal
Ubuntu 22.04 LTS is used below for the first introductory steps in working with Crystal. If you want to understand the examples with another operating system, you can find them on the Crystal-Website Installation instructions for various platforms.
For Ubuntu, there is an installation script that can be executed via cURL, which can be activated as follows:
curl -fsSL https://crystal-lang.org/install.sh | sudo bash
The tool downloads a recent Nightly version of Crystal, as shown in the following output:
crystal -vCrystal 1.6.2 [879691b2e] (2022-11-03)LLVM: 13.0.1Default target: x86_64-unknown-linux-gnu
Alternatively, stable or specifically selected Crystal versions can also be installed via the script using specific parameters. Further information can be found in the Installationsdokumentation.
Developers who prefer to get their software via snap can also use this packaging option. In the following examples, this is omitted to avoid complications with AppArmor and Co.
A full development environment (IDE) is also recommended when working with Crystal. In the world of Crystal, Microsoft’s open-source Visual Studio Code has established itself as a quasi-standard. It should also be used in the following steps.
After starting Visual Studio Code, the Crystal Language plug-in appears as a rotating icon. One click Install starts the deployment process typical of Visual Studio Code extensions.
If you also want to use IntelliSense, you must also install the Language Server module, which provides the IDE with the parsing information. It can currently be found in the GitHub-Repo von Julien Elbaz (elbywan).