Why should you learn Crystal?
Tags: crystal, programming, opensource, performance
Published: 2023-04-18 20:21:00

Have you ever heard of this amazing language called [Crystal](https://crystal-lang.org/)? Okay, let's understand a little bit about everything this amazing programming language has to offer.

The purpose of this short article is to talk a little about Crystal and why you might be interested in developing using it.

# What is Crystal?
Being a multi-paradigm programming language, for humans and computers, designed and developed by [Ary Borenszweig](https://github.com/asterite), [Juan Wajnerman](https://github.com/waj), [Brian Cardiff](https://github.com/bcardiff), in addition to more than 480 contributors to date, with syntax inspired by the Ruby language, Crystal stood out for applying concepts of concurrency and general use in a "differentiated" way, using static type checking through a global inference algorithm. Currently in active development, licensed under Apache 2.0.

# History
With the first works starting in 2011 with the aim of merging the productivity and elegance of Ruby with the speed, efficiency and security of a compiled language, the so-called Joy appears, which was later renamed to Crystal.

It's first compiler was written in Ruby, but rewritten in Crystal, enabling self-hosting from November 2013, with a first version released in June 2014, in addition to joining the TIOBE index in July 2016. It's first version stable appeared in March 2021, the famous version 1.0!

# Why is Crystal more efficient?
Simple, Crystal compiles native code using LLVM, excluding dynamic aspects of Ruby. It's advanced global type inference applied in the compiler, combined with [union types](https://crystal-lang.org/reference/1.8/syntax_and_semantics/union_types.html) creates a sense of a higher-level scripting language more than many other programming languages that can be compared.

In addition, Crystal has a macro system and supports method and operator overloading. It's concurrency model is inspired by sequential process communication (CSP) implementing green threads called fibers and other concepts inspired by the Go programming language. Another important point is it's automated garbage collection offering a Boehm collector.

# How to install?
Installation varies for each operating system to be used! You can try installing via your default package manager by searching for the package named `crystal` or else follow the [official installation guide](https://crystal-lang.org/install/)!

If you are using Windows, you can either install via WSL or try the trial version of the official installer for Windows!

# Shards
Shards is the name of the dependency manager in the Crystal programming language, which by default uses the `shard.yml` file to define project settings.

```yml
name: shards
version: 0.1.0

dependencies:
   openssl:
     github: datanoise/openssl.cr
     branch: master

development_dependencies:
   minitest:
     git: https://github.com/ysbaddaden/minitest.cr.git
     version: ~> 0.3.1

license: MIT
```

You can see the official repository by clicking [here](https://github.com/crystal-lang/shards).

# Code
Using the `.cr` file extension by default, let's create a practical example just showing the basis of a simple "Hello World", an HTTP server and a concurrency model!

## Hello World!
```cr
p "Hello World!"
# or
puts "Hello World!"
```

## HTTP server
```cr
require "http/server"

server = HTTP::Server.new do |context|
   context.response.content_type = "text/plain"
   context.response.print "Hello, got #{context.request.path}!"
end

puts "Listening on http://127.0.0.1:8080"
server.listen(8080)
```

Now just access the local page with port "8080" to test!

## Concurrency Model
```cr
channel = Channel(Int32).new
total_lines = 0
files = Dir.glob("*.txt")

files.each do |f|
   spawn of
     lines = File.read_lines(f)
     channel.send lines.size
   end
end

files.size.times do
   total_lines += channel.receive
end

puts total_lines
```

# Where can I see more about Crystal?
You can read the [official documentation](https://crystal-lang.org/reference/1.8/index.html), follow the official language profile on [GitHub](https://github.com/crystal-lang ) and even study my own guide in Portuguese to get started with Crystal: the famous [Crystal4noobs](https://github.com/lanjoni/crystal4noobs)!