Crystal (programming language)#Hello World

{{Short description|Object-oriented programming language}}

{{Infobox programming language

| name = Crystal

| logo = Crystal language logo.svg

| paradigm = Multi-paradigm: object-oriented, concurrent

| released = {{Start date and age|2014|06|19}}{{Cite web|url=https://crystal-lang.org/2014/06/19/crystal-0.1.0-released.html|title=Crystal 0.1.0 released!|website=crystal-lang|date=19 June 2014 }}

| designer = Ary Borenszweig, Juan Wajnerman, Brian Cardiff

| developer = Manas Technology Solutions

| latest release version = {{wikidata|property|preferred|references|edit|P348|P548=Q2804309}}

| latest release date = {{Start date and age|{{wikidata|qualifier|preferred|single|P348|P548=Q2804309|P577}}|df=yes}}

| typing = static, inferred, nominal, duck

| influenced by = Ruby, Go

| programming language = Crystal

| platform = IA-32 (i386), x86-64, AArch64

| operating system = Linux, macOS, FreeBSD, OpenBSD, Windows

| license = Apache License 2.0

| file ext = .cr

| website = {{URL|https://crystal-lang.org}}}}

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. 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. 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|lc=true}}. The first official version was released in June 2014. 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.{{cite web | url=https://forum.crystal-lang.org/t/crystal-multithreading-support/6622/19 | title=Crystal multithreading support | date=23 February 2024 }}

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

Adoption

In 2020, it was reported that the infotainment units in vehicles produced by Nikola Corporation were written in Crystal.{{cite web |last1=Pettinati |first1=Martin |title=Nikola Motor Company: Crystal powered dashboards on the trucks of the future {{!}} Manas.Tech |url=https://manas.tech/blog/2020/02/11/nikola-motor-company/ |website=Manas Technology Solutions |language=en |date=11 February 2020}} Much of the backend of the Kagi search engine is written with Crystal.{{cite web |title=Zac Nowicki – Tales from Kagi {{!}} CrystalConf 2023 |url=https://www.youtube.com/watch?v=r7t9xPajjTM |date=13 November 2023}}

Further reading

{{Refbegin}}

  • {{citation

| first1 = Simon

| last1 = St. Laurent

| first2 = Ivo

| last2 = Balbaert

| date = February 1, 2019

| title = Programming Crystal

| edition = P1.0

| publisher = Pragmatic Bookshelf

| isbn = 978-1-68050-286-2

| url = https://pragprog.com/book/crystal/programming-crystal

}}

  • {{citation

| first1 = George

| last1 = Dietrich

| first2 = Guilherme

| last2 = Bernal

| date = May 27, 2022

| title = Crystal Programming

| publisher = Packt Publishing

| isbn = 978-1801818674

}}

  • {{citation

| first1 = Ramon

| last1 = Wartala

| date = March 2016

| title = Die Ruby-artige Programmiersprache Crystal

| trans-title = The Ruby-like programming language Crystal

| work = Linux Magazin

| issue = 3/2016

| issn = 1432-640X

| language = de

| url = https://www.linux-magazin.de/ausgaben/2016/03/crystal/

}}

{{Refend}}

References

{{Reflist|refs=

{{cite web

| url = https://crystal-lang.org/reference/platform_support.html

| title = Crystal Platform Support

| website = crystal-lang.org

}}

{{cite web

| url = https://github.com/crystal-lang/crystal/graphs/contributors

| title = Contributors

| accessdate=July 25, 2019

| via = GitHub

}}

{{cite web

| url = http://crystal-lang.org/2013/09/23/type-inference-part-1.html

| title = Type inference part 1

| last = Brian J.

| first = Cardiff

| date = September 9, 2013

| website = crystal-lang.org

}}

{{cite web

| url = https://crystal-lang.org/2013/11/14/good-bye-ruby-thursday.html

| title = Good bye Ruby Thursday

| last = Borenszweig

| first = Ary

| date = November 14, 2013

| website = crystal-lang.org

}}

{{cite web

| url = https://crystal-lang.org/2014/06/19/crystal-0.1.0-released.html

| title = Crystal 0.1.0 released!

| last = Borenszweig

| first = Ary

| date = June 19, 2014

| website = crystal-lang.org

}}

{{cite web

| url = https://adlerhsieh.com/p/why-use-crystal-lang

| title = Why Crystal programming language?

| last = Hsieh

| first = Adler

| date = September 20, 2015

| website = adlerhsieh.com

}}

{{cite web

| url = https://manas.tech/blog/2016/04/01/the-story-behind-crystal/

| title = The story behind #CrystalLang

| last = David

| first = María Inti

| date = April 1, 2016

| website = manas.tech

}}

{{cite web

| url = http://crystal-lang.org/2016/06/14/crystal-0.18.0-released.html#comment-2732771703

| title = Crystal 0.18.0 released!

| last = Borenszweig

| first = Ary

| date = June 16, 2016

| website = crystal-lang.org

| quote =

}}

{{cite web

| url = https://devm.io/ruby/crystal-ruby-programming

| title = Programming with Crystal: 'A language for humans and computers'

| date = July 3, 2023

| website = devm.io

}}

}}