Crystal

Crystal

Source

Crystal’s syntax is heavily inspired by Ruby’s, so it feels natural to read and easy to write, and has the added benefit of a lower learning curve for experienced Ruby devs.

# 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 puts "Listening on http://127.0.0.1:8080"
server.listen(8080)

Start learning Crystal with the Language Reference

Crystal is statically type checked, so any type errors will be caught early by the compiler rather than fail on runtime. Moreover, and to keep the language clean, Crystal has built-in type inference, so most type annotations are unneeded.

def shout(x) # Notice that both Int32 and String respond_to `to_s` x.to_s.upcase
end foo = ENV["FOO"]? || 10 typeof(foo) # => (Int32 | String)
typeof(shout(foo)) # => String

Read more about Crystal's type system

All types are non-nilable in Crystal, and nilable variables are represented as a union between the type and nil. As a consequence, the compiler will automatically check for null references in compile time, helping prevent the dreadful billion-dollar mistake.

if rand(2) > 0 my_string = "hello world"
end puts my_string.upcase

Running the previous file:


$ crystal hello_world.cr
Error in hello_world.cr:5: undefined method 'upcase' for Nil (compile-time type is (String | Nil)) puts my_string.upcase ^~~~~~

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.

class Object def has_instance_var?(name) : Bool {{ @type.instance_vars.map &.name.stringify }}.includes? name end
end person = Person.new "John", 30
person.has_instance_var?("name") #=> true
person.has_instance_var?("birthday") #=> false

Read more about macros

Crystal uses green threads, called fibers, to achieve concurrency. Fibers communicate with each other using channels, as in Go or Clojure, without having to turn to shared memory or locks.

channel = Channel(Int32).new
total_lines = 0
files = Dir.glob("*.txt") files.each do |f| spawn do lines = File.read_lines(f) channel.send lines.size end
end files.size.times do total_lines += channel.receive
end puts total_lines

Read more about Crystal's concurrency model

Crystal has a dedicated syntax to easily call native libraries, eliminating the need to reimplement low-level tasks.

# Fragment of the BigInt implementation that uses GMP
@[Link("gmp")]
lib LibGMP alias Int = LibC::Int alias ULong = LibC::ULong struct MPZ _mp_alloc : Int32 _mp_size : Int32 _mp_d : ULong* end fun init_set_str = __gmpz_init_set_str(rop : MPZ*, str : UInt8*, base : Int) : Int fun cmp = __gmpz_cmp(op1 : MPZ*, op2 : MPZ*) : Int
end struct BigInt < Int def initialize(str : String, base = 10) err = LibGMP.init_set_str(out @mpz, str, base) raise ArgumentError.new("invalid BigInt: #{str}") if err == -1 end def <=>(other : BigInt) LibGMP.cmp(mpz, other) end
end

Learn how to bind to C libraries

Crystal libraries are packed as Shards, and distributed via Git without needing a centralised repository. Built in commands allow dependencies to be easily specified through a YAML file and fetched from their respective repositories.

name: my-project
version: 0.1
license: MIT crystal: 1.0.0 dependencies: mysql: github: crystal-lang/crystal-mysql

Read more about Shards in the repo

Crystal is licensed under the Apache License, Version 2.0 Crystal language, born & raised at Manas

Report Page