Crystal (programming language)
Crystal is a general-purpose, object-oriented programming language designed and developed by Ary Borenszweig and Juan Wajnerman and over one-hundred listed contributors.[1] Crystal is developed as open source software (under the Apache License, Version 2.0) with syntax inspired by Ruby. The language is statically type-checked but does not require that the type of variables or method arguments be specified. This is the result of advanced global type inference.[2] Its first official release was in June 2014.[3][4] While the original Crystal compiler was written in Ruby, in 2013 a new compiler written using the Crystal programming language itself was released.[5] The current release version is 0.15 and the language is in an active development phase.
Description
Although resembling the Ruby programming language in syntax, Crystal compiles to much more efficient native code using an LLVM backend, at the cost of disallowing the dynamic aspects of Ruby. However, the advanced global type inference used by the Crystal compiler, combined with the usage of union types, give Crystal the feel of a higher-level scripting language than many other comparable programming languages. Recent benchmarks have demonstrated that Crystal has a performance broadly similar to C for a wide range of computing tasks.[6][7][8] The language has automated garbage collection and currently offers a Boehm collector. Crystal possesses a macro system and supports generics and 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 the Go programming language.
Examples
Hello World
This is the simplest way to write the Hello World program in Crystal:
puts "Hello World!"
Or using an object-oriented programming style:
class Greeter
def initialize(name)
@name = name.capitalize
end
def salute
puts "Hello #{@name}!"
end
end
g = Greeter.new("world")
g.salute
HTTP Server
require "http/server"
server = HTTP::Server.new(8080) do |context|
context.response.content_type = "text/plain"
context.response.print "Hello world! The time is #{Time.now}"
end
puts "Listening on http://0.0.0.0:8080"
server.listen
Type Inference and Union Types
In the following code sample, there is no need to specify the type of method argument a, which will be inferred to be of a union type of all types provided to the print_max function.
def print_max(*a)
# The splat '*' indicates a vararg and print_max is a varidic function
# that takes a tuple of any size.
puts("#{a.max}") # Notice the use of string interpolation
end
print_max(5, 6, 3, 4.3, 9, 10, 7.9) # type of Array(Float64 | Int32)
print_max("lion", "rhinoceros", "zebra", "elephant") # type of Array(String)
print_max('1', 'a', 'i', '9') # type of Array(Char)
The code sample above prints 10, zebra, and i.
Concurrency
Channels can be used to communicate between fibers, which are initiated using the 'spawn' keyword.
channel = Channel(Int32).new
spawn do
puts "Before first send"
channel.send(1)
puts "Before second send"
channel.send(2)
end
puts "Before first receive"
value = channel.receive
puts value # => 1
puts "Before second receive"
value = channel.receive
puts value # => 2