GitHub Repository Forum RSS-Newsfeed

Crystal

A language for humans and computers

Examples

Crystal is a general-purpose, object-oriented programming language. With syntax inspired by Ruby, it’s a compiled language with static type-checking. Types are resolved by an advanced type inference algorithm.

# A very basic HTTP server
require "http/server"

server = HTTP::Server.new do |context|
  context.response.content_type = "text/plain"
  context.response.print "Hello world, got #{context.request.path}!"
end

address = server.bind_tcp(8080)
puts "Listening on http://#{address}"

# This call blocks until the process is terminated
server.listen

Batteries included

Crystal’s standard library comes with a whole range of libraries that let you start working on your project right away.

require "http/client"
require "json"

response = HTTP::Client.get("https://crystal-lang.org/api/versions.json")
json = JSON.parse(response.body)
version = json["versions"].as_a.find! { |entry| entry["released"]? != false }["name"]

puts "Latest Crystal version: #{version || "Unknown"}"

Type system

The compiler catches type errors early. Avoids null pointer exceptions at runtime.

The code is still clean and feels like a dynamic language.

def add(a, b)
  a + b
end

add 1, 2         # => 3
add "foo", "bar" # => "foobar"

Flow typing

The compiler tracks the type of variables at each point, and restricts types according to conditions.

loop do
  case message = gets # type is `String | Nil`
  when Nil
    break
  when ""
    puts "Please enter a message"
  else
    # In this branch, `message` cannot be `Nil` so we can safely call `String#upcase`
    puts message.upcase
  end
end

Concurrency Model

Crystal uses green threads, called fibers, to achieve concurrency. Fibers communicate with each other via channels without having to turn to shared memory or locks (CSP).

channel = Channel(Int32).new

3.times do |i|
  spawn do
    3.times do |j|
      sleep rand(100).milliseconds # add non-determinism for fun
      channel.send 10 * (i + 1) + j
    end
  end
end

9.times do
  puts channel.receive
end

C-bindings

Bindings for C libraries makes it easy to use existing tools. Crystal calls lib functions natively without any runtime overhead.

No need to implement the entire program in Crystal when there are already good libraries for some jobs.

# Define the lib bindings and link info:
@[Link("m")]
lib LibM
  fun pow(x : LibC::Double, y : LibC::Double) : LibC::Double
end

# Call a C function like a Crystal method:
puts LibM.pow(2.0, 4.0) # => 16.0

Macros

Crystal’s answer to metaprogramming is a powerful macro system, which ranges from basic templating and AST inspection, to types inspection and running arbitrary external programs.

macro upcase_getter(name)
  def {{ name.id }}
    @{{ name.id }}.upcase
  end
end

class Person
  upcase_getter name

  def initialize(@name : String)
  end
end

person = Person.new "John"
person.name # => "JOHN"

Dependencies

Crystal libraries are packed with Shards, a distributed dependency manager without a centralised repository.

It reads dependencies defined in shard.yml and fetches the source code from their repositories.

name: hello-world
version: 1.0.0
license: Apache-2.0

authors:
- Crys <crystal@manas.tech>

dependencies:
  mysql:
    github: crystal-lang/crystal-mysql
    version: ~>0.16.0

News


Sponsorship

Become a Crystal sponsor in only 3 simple steps via OpenCollective

Contribute
Enterprise support

Sponsoring Crystal creates a great springboard for your brand

Get Support
Hire us for your project

You can tap into the expertise of the very creators of the language to guide you in your implementation.

Hire Us

Top Sponsors

Success stories