Crystal (programming Language)
   HOME

TheInfoList



OR:

Crystal is a general-purpose,
object-oriented programming Object-oriented programming (OOP) is a programming paradigm based on the concept of "objects", which can contain data and code. The data is in the form of fields (often known as attributes or ''properties''), and the code is in the form of ...
language, designed and developed by Ary Borenszweig, Juan Wajnerman, Brian Cardiff and more than 300 contributors. With syntax inspired by the language
Ruby A ruby is a pinkish red to blood-red colored gemstone, a variety of the mineral corundum ( aluminium oxide). Ruby is one of the most popular traditional jewelry gems and is very durable. Other varieties of gem-quality corundum are called ...
, 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 Type inference refers to the automatic detection of the type of an expression in a formal language. These include programming languages and mathematical type systems, but also natural languages in some branches of computer science and linguistic ...
algorithm. Crystal is currently in active development. It is released as free and open-source software under the Apache License version 2.0.


History

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, as of November 2013. The first official version was released in June 2014. In July 2016, Crystal joined the
TIOBE index The TIOBE programming community index is a measure of popularity of programming languages, created and maintained by TIOBE Software BV, based in Eindhoven, the Netherlands. TIOBE stands for ''The Importance of Being Earnest'', the title of an 1 ...
.


Description

Although resembling the Ruby language in syntax, Crystal compiles to much more efficient native code using an
LLVM LLVM is a set of compiler and toolchain technologies that can be used to develop a front end for any programming language and a back end for any instruction set architecture. LLVM is designed around a language-independent intermediate repre ...
backend, at the cost of precluding the dynamic aspects of Ruby. The advanced global type inference used by the Crystal compiler, combined with
union type In computer science, a union is a value that may have any of several representations or formats within the same position in memory; that consists of a variable that may hold such a data structure. Some programming languages support special data ...
s, 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 In computer science, communicating sequential processes (CSP) is a formal language for describing patterns of interaction in concurrent systems. It is a member of the family of mathematical theories of concurrency known as process algebras, or ...
(CSP) and implements lightweight fibers and channels (for interfiber communication) inspired by Go.


Examples


Hello World

This is the simplest way to write the
Hello World ''Hello'' is a salutation or greeting in the English language. It is first attested in writing from 1826. Early uses ''Hello'', with that spelling, was used in publications in the U.S. as early as the 18 October 1826 edition of the '' Norwich ...
program in Crystal: puts "Hello World!" The same as in Ruby. Or using an
object-oriented programming Object-oriented programming (OOP) is a programming paradigm based on the concept of "objects", which can contain data and code. The data is in the form of fields (often known as attributes or ''properties''), and the code is in the form of ...
style: class Greeter def initialize(@name : String) end def salute puts "Hello #!" end end g = Greeter.new("world") g.salute


HTTP server

require "http/server" 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


TCP echo server

require "socket" def handle_client(client) message = client.gets client.puts message end server = TCPServer.new("localhost", 1234) while client = server.accept? spawn handle_client(client) end


Type inference and union types

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_000p typeof(desired_things.first) # typeof returns the compile time type, here (Symbol , String , Int32) p desired_things.first.class # the class method returns the runtime type, here Symbol


Concurrency

Channels can be used to communicate between fibers, which are initiated using the keyword spawn. 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


Further reading

* *


References


External links

* * {{Github, crystal-lang
/r/crystal_programming subreddit

Crystal Announcements
Object-oriented programming languages Programming languages created in 2014 Software using the Apache license Statically typed programming languages