Have you ever heard of this amazing language called Crystal? 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, Juan Wajnerman, Brian Cardiff, 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 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!
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.
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.
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!
p "Hello World!"
# or
puts "Hello World!"
HTTP server
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
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, follow the official language profile on GitHub and even study my own guide in Portuguese to get started with Crystal: the famous Crystal4noobs!
Top comments (2)
Congratulations friend, you are cool... Success
thanks a lot friend! hope you can enjoy the content! come be Crystalist!