Learn 12 programming languages in 23: Crystal
Crystal is a powerful and versatile language with a great documentation.
Hi đ, I am back to the #12in23, trying to learn 12 programming languages in 2023.
I am late đ because right now I only got 5/12 and we are in the month 9, but I will try to do it anyway. Crystal ?!
Crystal first impressions
I worked with Ruby on Rails in the past, and because the Crystal syntax is heavily inspired in the Ruby syntax it was really easy to pick it up.
The documentation is great. Itâs well written, and the website is responsive. So when I was reading (https://crystal-lang.org/) it was hard to stop.
Hello Crystal
puts "Hello World!"
Using a class:
class HelloWorld
def hello
"Hello, World!"
end
end
The documentation is so good that I could stop here, only add that itâs a great language to try and ask you to go read itâŚ
But I will add few words about it.
Good type system
You have flexibility and there is not need to set the type most of the time.
Crystal is statically type checked, so any type errors will be caught early by the compiler rather than fail on runtime. Moreover, and to keep the language clean, Crystal has built-in type inference, so most type annotations are unneeded.
class HelloWorld
def hello
local = 1
local = "lol"
puts local
end
end
helloClass = HelloWorld.new
helloClass.hello
# run at https://play.crystal-lang.org/#/r/fqsq
As you see there is the ClassName.new syntax that is odd to me, but I didnât have to declare the type of the variable local and could change the content from int to string without any issues.
def shout(x)
# Notice that both Int32 and String respond_to `to_s`
x.to_s.upcase
end
foo = ENV["FOO"]? || 10
typeof(foo) # => (Int32 | String)
typeof(shout(foo)) # => String
NULL REFERENCE CHECKS
All types are non-nilable in Crystal, and nilable variables are represented as a union between the type and nil. As a consequence, the compiler will automatically check for null references in compile time, helping prevent the dreadful billion-dollar mistake.
The language will get null (nil) errors at compile time.
No try catch.
For I Java programmer like me that itâs a dream.
Crystal good parts
- The Assignment system is really good: there are options to do Combined Assignment, Chained assignment, Multiple Assignment, One-to-many assignment, Splat assignment and Using Underscore.
What about one line to swap two variable values?
a = 1
b = 2
a, b = b, a
puts a # => 2
puts b # => 1
# run at https://play.crystal-lang.org/#/r/fqvu
That is really good, not have to assign an auxiliary variable itâs amazing.
- The libraries are packed as Shards: Crystal libraries are packed as Shards, and distributed via Git without needing a centralised repository. Built in commands allow dependencies to be easily specified through a YAML file and fetched from their respective repositories.
name: my-project
version: 0.1
license: MIT
crystal: 1.9.2
dependencies:
mysql:
github: crystal-lang/crystal-mysql
If you love a centralised repository this is a bad thing, I donât.
- Syntax is very clean: apart from the Class.new instead of new Class (like Java), it has a concise and expressive syntax that makes it easy to write readable and maintainable code.
- Performance: Crystal is a very fast language, thanks to its use of the LLVM compiler infrastructure. It is often said to be as fast as C or C++, but with a much cleaner syntax.
I could not verify that yet but I was interested in Crystal first because of his performance, a Tweet from Fabio Akita got my attention:
He created a project using Lucky, with the language Crystal to run against a Gatling stress-test scenario.
Exercism to Learn Crystal
Once more I used Exercism to practice and learn more about Crystal.
I finished few problems that covered the Fundamental Mode in Crystal.
Exercism still a really good website to practice code.
Overall, Crystal is a powerful and versatile language that is well-suited for a wide variety of projects. If you are looking for a language that is fast, safe, expressive, and interoperable, then Crystal is a good choice.
I really believe Crystal deserves more â¤ď¸.