BT

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

Bookmarks

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.

We need your feedback

How might we improve InfoQ for you

Thank you for being an InfoQ reader.

Each year, we seek feedback from our readers to help us improve InfoQ. Would you mind spending 2 minutes to share your feedback in our short survey? Your feedback will directly help us continually evolve how we support you.

Take the Survey

Rate this Article

Adoption
Style

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Community comments

  • Congratulations on the GA release!

    by Cameron Purdy,

    Your message is awaiting moderation. Thank you for participating in the discussion.

    I'm glad to see Crystal getting some love here!

    I've been watching Crystal grow for a few years. Great to see a nice alternative to C (and now C++) getting some traction, almost 50 years on! I do like the ideas behind it, and the team working on it. Also, it's great that it didn't come out of Silicon Valley :-)

  • Fibonacci example error

    by Phil Kodi,

    Your message is awaiting moderation. Thank you for participating in the discussion.

    Calling fib() function while defined fibonacci()

  • Re: Fibonacci example error

    by Olivier Couriol,

    Your message is awaiting moderation. Thank you for participating in the discussion.

    Good catch! This is now corrected. Thanks for the heads-up.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

BT

Is your profile up-to-date? Please take a moment to review and update.

Note: If updating/changing your email, a validation request will be sent

Company name:
Company role:
Company size:
Country/Zone:
State/Province/Region:
You will be sent an email to validate the new email address. This pop-up will close itself in a few moments.