| Crystal | |
|---|---|
| Paradigm | Multi-paradigm:object-oriented,concurrent |
| Designed by | Ary Borenszweig, Juan Wajnerman, Brian Cardiff |
| Developer | Manas Technology Solutions |
| First appeared | June 19, 2014; 11 years ago (2014-06-19)[1] |
| Stable release | |
| Typing discipline | static,inferred,nominal,duck |
| Implementation language | Crystal |
| Platform | IA-32 (i386),x86-64,AArch64[2] |
| OS | Linux,macOS,FreeBSD,OpenBSD,Windows[2] |
| License | Apache License 2.0 |
| Filename extensions | .cr |
| Website | crystal-lang |
| Influenced by | |
| Ruby,Go[3] | |
Crystal is ahigh-levelgeneral-purpose,object-oriented programming language, designed and developed by Ary Borenszweig, Juan Wajnerman, Brian Cardiff and more than 400 contributors.[4] With syntax inspired by the languageRuby,[5] it is acompiled language withstatic type-checking, but specifying the types of variables or method arguments is generally unneeded. Types are resolved by an advanced globaltype inference algorithm.[6][7] Crystal is currently in active development. It is released asfree and open-source software under theApache License version 2.0.
Work on the language began in June 2011,[8] with the aim of merging the elegance and productivity of Ruby with the speed, efficiency, and type safety of a compiled language.[9][8] Initially namedJoy, it was quickly renamed toCrystal.[8]
The Crystal compiler was first written in Ruby, but later rewritten in Crystal, thus becomingself-hosting, as of November 2013[update].[10] The first official version was released in June 2014.[11] In July 2016, Crystal joined theTIOBE index.
Although resembling the Ruby language in syntax, Crystal compiles to much more efficient native code using anLLVM backend, at the cost of precluding the dynamic aspects of Ruby. The advanced global type inference used by the Crystal compiler, combined withunion 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 aBoehm collector. Crystal possesses a macro system and supports generics as well as method and operator overloading. Its concurrency model is inspired bycommunicating sequential processes (CSP) and implements lightweight fibers and channels (for interfiber communication), inspired byGo.[3]
This is the simplest way to write theHello World program in Crystal:
puts"Hello World!"
The same as in Ruby.
Or using anobject-oriented programming style:
classGreeterdefinitialize(@name:String)enddefsaluteputs"Hello#{@name}!"endendg=Greeter.new("world")g.salute
require"http/server"server=HTTP::Server.newdo|context|context.response.content_type="text/plain"context.response.print"Hello world! The time is#{Time.local}"endserver.bind_tcp("0.0.0.0",8080)puts"Listening on http://0.0.0.0:8080"server.listen
require"socket"defhandle_client(client)message=client.getsclient.putsmessageendserver=TCPServer.new("localhost",1234)whileclient=server.accept?spawnhandle_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.
desired_things=[:unicorns,"butterflies",1_000_000]ptypeof(desired_things.first)# typeof returns the compile time type, here (Symbol | String | Int32)pdesired_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 keywordspawn.
channel=Channel(Int32).newspawndoputs"Before first send"channel.send(1)puts"Before second send"channel.send(2)endputs"Before first receive"value=channel.receiveputsvalue# => 1puts"Before second receive"value=channel.receiveputsvalue# => 2
In 2020, it was reported that the infotainment units in vehicles produced byNikola Corporation were written in Crystal.[12] Much of the backend of theKagi search engine is written with Crystal.[13]