Crystal (programming language)

From Wikipedia, the free encyclopedia
Jump to navigation Jump to search
Crystal language logo.svg
ParadigmMulti-paradigm: object-oriented, concurrent
Designed byAry Borenszweig, Juan Wajnerman, Brian Cardiff
DeveloperManas Technology Solutions
First appearedJune 19, 2014; 8 years ago (2014-06-19)[1]
Stable release
1.5.0[2] Edit this on Wikidata / 6 July 2022; 43 days ago (6 July 2022)
Typing disciplinestatic, inferred, nominal, duck
Implementation languageCrystal
PlatformIA-32 (i386), x86-64, AArch64[3]
OSLinux, macOS, FreeBSD, OpenBSD[3]
LicenseApache License 2.0
Influenced by
Ruby, C, Rust, Go,[4] C#,[4] Python[4]

Crystal is a general-purpose, object-oriented programming language, designed and developed by Ary Borenszweig, Juan Wajnerman, Brian Cardiff and more than 300 contributors.[5] With syntax inspired by the language Ruby,[4] it is a compiled language with static type-checking, but specifying the types of variables or method arguments is generally unneeded. Types are resolved by an advanced global type inference algorithm.[6] Crystal is currently in active development. It is released as free and open-source software under the Apache License version 2.0.


Work on the language began in June 2011,[7] with the aim of merging the elegance and productivity of Ruby with the speed, efficiency, and type safety of a compiled language.[8][7] Initially named Joy, it was quickly renamed to Crystal.[7]

The Crystal compiler was first written in Ruby, but later rewritten in Crystal, thus becoming self-hosting, as of November 2013.[9] The first official version was released in June 2014.[10] In July 2016, Crystal joined the TIOBE index.


Although resembling the Ruby language in syntax, Crystal compiles to much more efficient native code using an LLVM backend, at the cost of precluding the dynamic aspects of Ruby. However, the advanced global type inference used by the Crystal compiler, combined with the use of union types, gives Crystal the feel of a higher-level scripting language more so than many other comparable programming languages. The language has automated garbage collection and currently offers a Boehm collector. Crystal possesses a macro system and supports generics as well as both method and operator overloading. Crystal's concurrency model is inspired by communicating sequential processes (CSP) and implements light-weight fibers and channels (for communicating between fibers) inspired by Go.[4]


Hello World[edit]

This is the simplest way to write the Hello World program in Crystal:

puts "Hello World!"

The same as in Ruby.

Or using an object-oriented programming style:

class Greeter
  def initialize(@name : String)

  def salute
    puts "Hello #{@name}!"

g ="world")

HTTP server[edit]

require "http/server"

server = do |context|
  context.response.content_type = "text/plain"
  context.response.print "Hello world! The time is #{Time.local}"

server.bind_tcp("", 8080)
puts "Listening on"

TCP echo server[edit]

require "socket"

def handle_client(client)
  message = client.gets
  client.puts message

server ="localhost", 1234)
while client = server.accept?
  spawn handle_client(client)

Type inference and union types[edit]

The following code defines an array containing different types with no usable common ancestor. Crystal automatically creates a union type out of the types of the individual items.

desired_things = [:unicorns, "butterflies", 1_000_000]
p typeof(desired_things.first) # typeof returns the compile time type, here (Symbol | String | Int32)
p desired_things.first.class   # the class method returns the runtime type, here Symbol


Channels can be used to communicate between fibers, which are initiated using the keyword spawn.

channel = Channel(Int32).new

spawn do
  puts "Before first send"
  puts "Before second send"

puts "Before first receive"
value = channel.receive
puts value # => 1

puts "Before second receive"
value = channel.receive
puts value # => 2

Further reading[edit]

  • St. Laurent, Simon; Balbaert, Ivo (February 1, 2019), Programming Crystal (P1.0 ed.), Pragmatic Bookshelf, ISBN 978-1-68050-286-2
  • Wartala, Ramon (March 2016), "Die Ruby-artige Programmiersprache Crystal" [The Ruby-like programming language Crystal], Linux Magazin (in German), no. 3/2016, ISSN 1432-640X


  1. ^ "Crystal 0.1.0 released!". crystal-lang.
  2. ^ "Release 1.5.0".
  3. ^ a b "Crystal Platform Support".
  4. ^ a b c d e Borenszweig, Ary (June 16, 2016). "Crystal 0.18.0 released!". It's heavily inspired by Ruby, and other languages (like C#, Go and Python).
  5. ^ "Contributors". Retrieved July 25, 2019 – via GitHub.
  6. ^ Brian J., Cardiff (September 9, 2013). "Type inference part 1".
  7. ^ a b c David, María Inti (April 1, 2016). "The story behind #CrystalLang".
  8. ^ Hsieh, Adler (September 20, 2015). "Why Crystal programming language?".
  9. ^ Borenszweig, Ary (November 14, 2013). "Good bye Ruby Thursday".
  10. ^ Borenszweig, Ary (June 19, 2014). "Crystal 0.1.0 released!".

External links[edit]