Crystal (programming language)

From Wikipedia, the free encyclopedia
Jump to navigation Jump to search
Crystal
Crystal language logo.svg
Paradigm Multi-paradigm: object-oriented
Designed by Ary Borenszweig, Juan Wajnerman, Brian Cardiff
Developer Manas Technology Solutions
First appeared June 18, 2014; 4 years ago (2014-06-18)
Preview release
0.26.1 / August 28, 2018; 13 days ago (2018-08-28)[1]
Typing discipline static
Implementation language Crystal
Platform IA-32 (i386), x86-64
OS Linux, macOS (Homebrew) [2]
License Apache License 2.0
Filename extensions .cr
Website crystal-lang.org
Influenced by
Ruby,[3] C, Rust, Go,[3] C#,[3] Python[3]

Crystal is a general-purpose, object-oriented programming language, designed and developed by Ary Borenszweig, Juan Wajnerman, Brian Cardiff and more than 300 contributors.[4] 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.[5] Crystal is in active development. It is released as free and open-source software under the Apache License version 2.0.

History[edit]

Work on the language began in June 2011,[6] with the purpose of creating a language with the elegance and productivity of Ruby and the speed, efficiency, and type safety of a compiled language.[7][6] Initially named Joy, it was quickly renamed to Crystal.[6]

The Crystal compiler was first written in Ruby, but later rewritten in Crystal, thus becoming self-hosting, as of November 2013.[8] The first official version was released in June 2014.[9] In July 2016, Crystal joined the TIOBE index.

Description[edit]

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. However, the advanced global type inference used by the Crystal compiler, combined with the use of union types, gives Crystal the feel of a higher-level scripting language more so than many other comparable programming languages. 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 language Go.[3]

Examples[edit]

Hello World[edit]

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 : String)
  end

  def salute
    puts "Hello #{@name}!"
  end
end

g = Greeter.new("world")
g.salute

HTTP server[edit]

require "http/server"

server = HTTP::Server.new do |context|
  context.response.content_type = "text/plain"
  context.response.print "Hello world! The time is #{Time.now}"
end

server.bind_tcp("0.0.0.0", 8080)
puts "Listening on http://0.0.0.0:8080"
server.listen

TCP echo server[edit]

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[edit]

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

Concurrency[edit]

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

References[edit]

  1. ^ "Releases - crystal-lang/crystal". Retrieved 8 July 2018 – via GitHub. 
  2. ^ [1]
  3. ^ a b c d e Borenszweig, Ary. "Crystal 0.18.0 released!". It's heavily inspired by Ruby, and other languages (like C#, Go and Python). 
  4. ^ Contributors on git repository
  5. ^ Type inference part 1
  6. ^ a b c David, María Inti. "The story behind #CrystalLang". 
  7. ^ Hsieh, Adler. "Why Crystal programming language?". 
  8. ^ Borenszweig, Ary. "Good bye Ruby Thursday". 
  9. ^ Borenszweig, Ary. "Crystal 0.1.0 released!". 

External links[edit]