Crystal (programming language)

From HandWiki - Reading time: 3 min

Short description: Object-oriented programming language
Crystal
Crystal language logo.svg
ParadigmMulti-paradigm: object-oriented, concurrent
Designed byAry Borenszweig, Juan Wajnerman, Brian Cardiff
DeveloperManas Technology Solutions
First appearedJune 19, 2014; 10 years ago (2014-06-19)[1]
Typing disciplinestatic, inferred, nominal, duck
Implementation languageCrystal
PlatformIA-32 (i386), x86-64, AArch64[2]
OSLinux, macOS, FreeBSD, OpenBSD[2]
LicenseApache License 2.0
Filename extensions.cr
Websitecrystal-lang.org
Influenced by
Ruby, C, Rust, Go,[3] C#,[3] Python[3]

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.[4] With syntax inspired by the language Ruby,[3] 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][6] 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,[7] with the aim of merging the elegance and productivity of Ruby with the speed, efficiency, and type safety of a compiled language.[8][7] Initially named Joy, it was quickly renamed to Crystal.[7]

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

Description

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

Examples

Hello World

This is the simplest way to write the Hello World program in Crystal:

puts "Hello World!"

The same as in Ruby.

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 do |context|
  context.response.content_type = "text/plain"
  context.response.print "Hello world! The time is #{Time.local}"
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_000]
p 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




Licensed under CC BY-SA 3.0 | Source: https://handwiki.org/wiki/Crystal_(programming_language)
12 views | Status: cached on July 15 2024 20:22:47
↧ Download this article as ZWI file
Encyclosphere.org EncycloReader is supported by the EncyclosphereKSF