BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News Crystal Language That Aims at C Performance with Ruby Syntax Releases 1.0

Crystal Language That Aims at C Performance with Ruby Syntax Releases 1.0

This item in japanese

Crystal, a new object-oriented, compiled systems programming language that aims to blend the conciseness and friendliness of Ruby with the efficiency of C, recently released its first major version. Crystal 1.0 has a syntax close to Ruby’s and features statically inferred types, C bindings, and macros. Crystal may attract developers with a Ruby/Rails, Elixir/Phoenix background.

The Crystal team explained the rationale behind the new language as follows:

We love Ruby’s efficiency for writing code.

We love C’s efficiency for running code.

We want the best of both worlds.

We want the compiler to understand what we mean without having to specify types everywhere.

We want full OOP.

Oh, and we don’t want to write C code to make the code run faster.

While full compatibility with Ruby is stated as a non-goal, Crystal has a syntax similar to Ruby’s. The Fibonnaci function can be written in Crystal as follows:

def fib(n : UInt64)
  return 1_u64 if n <= 1
  fib(n - 1) + fib(n - 2)
end

puts fib(46)

Crystal compiles to native code using LLVM under the hood. While Crystal does not publish any benchmark on its site, the user community has reported encouraging results, some of which have been highly commented on on Hacker News.

Everything in Crystal is an object, i.e. an entity that has a type and that responds to some methods. The pieces of state encapsulated in objects can only be accessed by invoking methods. Crystal’s type system allows null reference checks, method (and operator) overloading, union types, generics, enums, aliases, splats, tuples, and more. The compiler uses type inference to minimize the need for developers to explicitly write types. Developers may however have to help the compiler with type annotations in some occurrences. Explicit types additionally can be useful as documentation and may speed up significantly compilation times.

Crystal lets developers reuse the large existing C ecosystem with C bindings, without having to write C code. An example is as follows (assuming the say_hi_c directory contains a C object file that contains a hi function):

require "./say_hi_c/*"

@[Link(ldflags: "#{__DIR__}/say_hi_c/sayhi.o")]

lib Say
  fun hi(name : LibC::Char*) : Void
end

Say.hi("Auth0")

Crystal’s macro system simplifies code generation and may be used to reduce boilerplate. Crystal currently supports message-driven concurrency (with fibers communicating via channels ) but not parallelism. The ability to run the Crystal runtime on multiple cores is on the roadmap, together with ARM support, and availability on Windows. Building fully statically linked executables is currently only supported on Alpine Linux.

One developer presented a possible Crystal use case as follows:

When it comes to writing low-level systems such as daemons and obtuse Kernels, while it would be most performant to turn to C – it’d also take me a LONG time to achieve relatively little and the aforementioned tears would be very likely flowing. This is where Crystal comes in.

Another developer that used Crystal to demonstrate how to create a cryptocurrency abounded in the same direction:

For a better demonstration, I wanted to use a productive language like Ruby without compromising the performance. Cryptocurrency has many time-consuming computations (namely mining and hashing), and that’s why compiled languages such as C++ and Java are the languages of choice for building “real” cryptocurrencies. That being said, I wanted to use a language with a cleaner syntax so I could keep development fun and allow better readability. Crystal’s performance tends to be good anyway.

Developers may build web applications with a growing ecosystem of web frameworks, the most notable being Amber, Lucky, and Kemal.

The Crystal language (originally named Joy) originated in 2011, with the original goals of combining the productivity of Ruby with the speed and type safety of a compiled language. The first official version was released in 2014. Crystal 1.0 was released in March 2021. Crystal is licensed under the Apache License, Version 2.0. Developers can quickly try Crystal with the online playground.

Rate this Article

Adoption
Style

BT