Jump to content

Crystal (programming language)

From Wikipedia, the free encyclopedia

This is an old revision of this page, as edited by $traight-$hoota (talk | contribs) at 09:17, 14 July 2017 (minor improvements to language and syntax). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

Crystal
File:The Crystal programming language logo.svg
ParadigmMulti-paradigm: Object-oriented
Designed byAry Borenszweig
DeveloperManas Technology Solutions
First appearedJune 18, 2014; 10 years ago (2014-06-18)
Preview release
0.23.0 / June 27, 2017; 7 years ago (2017-06-27)
Typing disciplinestatic
Implementation languageCrystal
Platform'IA-32' (i386), 'x86-64'
OSOS X, Linux, FreeBSD
LicenseApache License 2.0
Filename extensions.cr
Websitecrystal-lang.org
Influenced by
Ruby,[1] C, Rust, Go,[1] C#,[1] Python[1]

Crystal is a general-purpose, object-oriented programming language designed and developed by Ary Borenszweig and Juan Wajnerman and more than two-hundred contributors.[2] Crystal is developed as open source software under the Apache License Version 2.0 with syntax inspired by Ruby. It is a compiled language with static type-checking but it is generally not required to specify the type of variables or method arguments. The types are resolved by an advanced global type inference algorithm.[3] The language is in an active development phase.

History

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

In November 2013, the Crystal compiler (previously written in Ruby) became self-hosting.[6] The first official version was released in June 2014.[7] In July 2016 Crystal was added to the TIOBE Index.

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. 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.[1]

Examples

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

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

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

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

References

  1. ^ 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).
  2. ^ Contributers on git repository
  3. ^ Type inference part 1
  4. ^ a b c David, María Inti. "The story behind #CrystalLang".
  5. ^ Hsieh, Adler. "Why Crystal programming language?".
  6. ^ Borenszweig, Ary. "Good bye Ruby Thursday".
  7. ^ Borenszweig, Ary. "Crystal 0.1.0 released!".