Crystal (programming language)

Last updated
Crystal
Crystal language logo.svg
Paradigm Multi-paradigm: object-oriented, concurrent
Designed by Ary Borenszweig, Juan Wajnerman, Brian Cardiff
Developer Manas Technology Solutions
First appearedJune 19, 2014;9 years ago (2014-06-19) [1]
Stable release
1.11.2 [2]   OOjs UI icon edit-ltr-progressive.svg / 18 January 2024;2 months ago (18 January 2024)
Typing discipline static, inferred, nominal, duck
Implementation languageCrystal
Platform IA-32 (i386), x86-64, AArch64 [3]
OS Linux, macOS, FreeBSD, OpenBSD, Windows [3]
License Apache License 2.0
Filename extensions .cr
Website crystal-lang.org
Influenced by
Ruby, C, Rust, Go, [4] C#, [4] Python [4]

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. [5] With syntax inspired by the language Ruby, [4] 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. [6] [7] Crystal is currently in active development. It is released as free and open-source software under the Apache License version 2.0.

Contents

History

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

The Crystal compiler was first written in Ruby, but later rewritten in Crystal, thus becoming self-hosting, as of November 2013. [10] The first official version was released in June 2014. [11] 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. [4]

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:

classGreeterdefinitialize(@name:String)enddefsaluteputs"Hello #{@name}!"endendg=Greeter.new("world")g.salute

HTTP server

require"http/server"server=HTTP::Server.newdo|context|context.response.content_type="text/plain"context.response.print"Hello world! The time is #{Time.local}"endserver.bind_tcp("0.0.0.0",8080)puts"Listening on http://0.0.0.0:8080"server.listen

TCP echo server

require"socket"defhandle_client(client)message=client.getsclient.putsmessageendserver=TCPServer.new("localhost",1234)whileclient=server.accept?spawnhandle_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]ptypeof(desired_things.first)# typeof returns the compile time type, here (Symbol | String | Int32)pdesired_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).newspawndoputs"Before first send"channel.send(1)puts"Before second send"channel.send(2)endputs"Before first receive"value=channel.receiveputsvalue# => 1puts"Before second receive"value=channel.receiveputsvalue# => 2

Further reading

Related Research Articles

Common Intermediate Language (CIL), formerly called Microsoft Intermediate Language (MSIL) or Intermediate Language (IL), is the intermediate language binary instruction set defined within the Common Language Infrastructure (CLI) specification. CIL instructions are executed by a CIL-compatible runtime environment such as the Common Language Runtime. Languages which target the CLI compile to CIL. CIL is object-oriented, stack-based bytecode. Runtimes typically just-in-time compile CIL instructions into native code.

<span class="mw-page-title-main">Visual Basic (.NET)</span> Object-oriented computer programming language

Visual Basic (VB), originally called Visual Basic .NET (VB.NET), is a multi-paradigm, object-oriented programming language, implemented on .NET, Mono, and the .NET Framework. Microsoft launched VB.NET in 2002 as the successor to its original Visual Basic language, the last version of which was Visual Basic 6.0. Although the ".NET" portion of the name was dropped in 2005, this article uses "Visual Basic [.NET]" to refer to all Visual Basic languages released since 2002, in order to distinguish between them and the classic Visual Basic. Along with C# and F#, it is one of the three main languages targeting the .NET ecosystem. Microsoft updated its VB language strategy on 6 February 2023, stating that VB is a stable language now and Microsoft will keep maintaining it.

In computer science, reflective programming or reflection is the ability of a process to examine, introspect, and modify its own structure and behavior.

<span class="mw-page-title-main">Apache Groovy</span> Programming language

Apache Groovy is a Java-syntax-compatible object-oriented programming language for the Java platform. It is both a static and dynamic language with features similar to those of Python, Ruby, and Smalltalk. It can be used as both a programming language and a scripting language for the Java Platform, is compiled to Java virtual machine (JVM) bytecode, and interoperates seamlessly with other Java code and libraries. Groovy uses a curly-bracket syntax similar to Java's. Groovy supports closures, multiline strings, and expressions embedded in strings. Much of Groovy's power lies in its AST transformations, triggered through annotations.

Object Pascal is an extension to the programming language Pascal that provides object-oriented programming (OOP) features such as classes and methods.

Boo is an object-oriented, statically typed, general-purpose programming language that seeks to make use of the Common Language Infrastructure's support for Unicode, internationalization, and web applications, while using a Python-inspired syntax and a special focus on language and compiler extensibility. Some features of note include type inference, generators, multimethods, optional duck typing, macros, true closures, currying, and first-class functions.

Expect is an extension to the Tcl scripting language written by Don Libes. The program automates interactions with programs that expose a text terminal interface. Expect, originally written in 1990 for the Unix platform, has since become available for Microsoft Windows and other systems.

<span class="mw-page-title-main">Scala (programming language)</span> General-purpose programming language

Scala is a strong statically typed high-level general-purpose programming language that supports both object-oriented programming and functional programming. Designed to be concise, many of Scala's design decisions are intended to address criticisms of Java.

<span class="mw-page-title-main">Oxygene (programming language)</span> Object Pascal-based programming language

Oxygene is a programming language developed by RemObjects Software for Microsoft's Common Language Infrastructure, the Java Platform and Cocoa. Oxygene is based on Delphi's Object Pascal, but also has influences from C#, Eiffel, Java, F# and other languages.

<span class="mw-page-title-main">Vala (programming language)</span> Programming language

Vala is an object-oriented programming language with a self-hosting compiler that generates C code and uses the GObject system.

<span class="mw-page-title-main">Rack (web server interface)</span> API specification for web applications in programming language Ruby

Rack is a modular interface between web servers and web applications developed in the Ruby programming language. With Rack, application programming interfaces (APIs) for web frameworks and middleware are wrapped into a single method call handling HTTP requests and responses.

<span class="mw-page-title-main">Vaadin</span>

Vaadin is an open-source web application development platform for Java. Vaadin includes a set of Web Components, a Java web framework, and a set of tools that enable developers to implement modern web graphical user interfaces (GUI) using the Java programming language only, TypeScript only, or a combination of both.

<span class="mw-page-title-main">Go (programming language)</span> Programming language

Go is a statically typed, compiled high-level programming language designed at Google by Robert Griesemer, Rob Pike, and Ken Thompson. It is syntactically similar to C, but also has memory safety, garbage collection, structural typing, and CSP-style concurrency. It is often referred to as Golang because of its former domain name, golang.org, but its proper name is Go.

RabbitMQ is an open-source message-broker software that originally implemented the Advanced Message Queuing Protocol (AMQP) and has since been extended with a plug-in architecture to support Streaming Text Oriented Messaging Protocol (STOMP), MQ Telemetry Transport (MQTT), and other protocols.

Nemerle is a general-purpose, high-level, statically typed programming language designed for platforms using the Common Language Infrastructure (.NET/Mono). It offers functional, object-oriented, aspect-oriented, reflective and imperative features. It has a simple C#-like syntax and a powerful metaprogramming system.

<span class="mw-page-title-main">Opa (programming language)</span>

Opa is an open-source programming language for developing scalable web applications.

<span class="mw-page-title-main">Elm (programming language)</span> Functional programming language

Elm is a domain-specific programming language for declaratively creating web browser-based graphical user interfaces. Elm is purely functional, and is developed with emphasis on usability, performance, and robustness. It advertises "no runtime exceptions in practice", made possible by the Elm compiler's static type checking.

<span class="mw-page-title-main">Vert.x</span>

Eclipse Vert.x is a polyglot event-driven application framework that runs on the Java Virtual Machine.

Elixir is a functional, concurrent, high-level general-purpose programming language that runs on the BEAM virtual machine, which is also used to implement the Erlang programming language. Elixir builds on top of Erlang and shares the same abstractions for building distributed, fault-tolerant applications. Elixir also provides tooling and an extensible design. The latter is supported by compile-time metaprogramming with macros and polymorphism via protocols.

<span class="mw-page-title-main">Nim (programming language)</span> Programming language

Nim is a general-purpose, multi-paradigm, statically typed, compiled high-level systems programming language, designed and developed by a team around Andreas Rumpf. Nim is designed to be "efficient, expressive, and elegant", supporting metaprogramming, functional, message passing, procedural, and object-oriented programming styles by providing several features such as compile time code generation, algebraic data types, a foreign function interface (FFI) with C, C++, Objective-C, and JavaScript, and supporting compiling to those same languages as intermediate representations.

References

  1. "Crystal 0.1.0 released!". crystal-lang. 19 June 2014.
  2. "Release 1.11.2". 18 January 2024. Retrieved 19 January 2024.
  3. 1 2 "Crystal Platform Support". crystal-lang.org.
  4. 1 2 3 4 5 Borenszweig, Ary (June 16, 2016). "Crystal 0.18.0 released!". crystal-lang.org. It's heavily inspired by Ruby, and other languages (like C#, Go and Python).
  5. "Contributors" . Retrieved July 25, 2019 via GitHub.
  6. Brian J., Cardiff (September 9, 2013). "Type inference part 1". crystal-lang.org.
  7. "Programming with Crystal: 'A language for humans and computers'". devm.io. July 3, 2023.
  8. 1 2 3 David, María Inti (April 1, 2016). "The story behind #CrystalLang". manas.tech.
  9. Hsieh, Adler (September 20, 2015). "Why Crystal programming language?". motion-express.com.
  10. Borenszweig, Ary (November 14, 2013). "Good bye Ruby Thursday". crystal-lang.org.
  11. Borenszweig, Ary (June 19, 2014). "Crystal 0.1.0 released!". crystal-lang.org.