Crystal | |
Paradigm: | Multi-paradigm |
Released: | [1] |
Designer: | Ary Borenszweig, Juan Wajnerman, Brian Cardiff |
Developer: | Manas Technology Solutions |
Typing: | static, inferred, nominal, duck |
Influenced By: | Ruby, C, Rust, Go, C#, Python |
Programming Language: | Crystal |
Platform: | IA-32 (i386), x86-64, AArch64 |
Operating System: | Linux, macOS, FreeBSD, OpenBSD, Windows |
License: | Apache License 2.0 |
File Ext: | .cr |
Crystal is a high-level general-purpose, object-oriented programming language, designed and developed by Ary Borenszweig, Juan Wajnerman, Brian Cardiff and more than 400 contributors. With syntax inspired by the language Ruby, 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. 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, with the aim of merging the elegance and productivity of Ruby with the speed, efficiency, and type safety of a compiled language. Initially named Joy, it was quickly renamed to Crystal.
The Crystal compiler was first written in Ruby, but later rewritten in Crystal, thus becoming self-hosting, . The first official version was released in June 2014. 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. The advanced global type inference used by the Crystal compiler, combined with union types, gives it more the feel of a higher-level scripting language than many other comparable programming languages. It has automated garbage collection and offers a Boehm collector. Crystal possesses a macro system and supports generics as well as method and operator overloading. Its concurrency model is inspired by communicating sequential processes (CSP) and implements lightweight fibers and channels (for interfiber communication) inspired by Go.
This is the simplest way to write the Hello World program in Crystal:
Or using an object-oriented programming style:
def salute puts "Hello #!" endend
g = Greeter.new("world")g.salute
server = HTTP::Server.new do |context| context.response.content_type = "text/plain" context.response.print "Hello world! The time is #"end
server.bind_tcp("0.0.0.0", 8080)puts "Listening on http://0.0.0.0:8080"server.listen
def handle_client(client) message = client.gets client.puts messageend
server = TCPServer.new("localhost", 1234)while client = server.accept? spawn handle_client(client)end
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.
Channels can be used to communicate between fibers, which are initiated using the keyword spawn
.
spawn do puts "Before first send" channel.send(1) puts "Before second send" channel.send(2)end
puts "Before first receive"value = channel.receiveputs value # => 1
puts "Before second receive"value = channel.receiveputs value # => 2