I’ve been trying out Crystal Lang for a few months now and I have to admit that, I am really happy with it, even if it’s not a functional language nor has a very strict compiler. The reason why is pretty simple: it’s as easy to write as any dynamic language like Ruby, Python or PHP while still providing a compiler that is helpful. Kudos to the Crystal team for that!
Coming from Rust and Python
Last time, I wrote a blog post where I explained how I came from Python to Rust. Here is a quick summary: I love when a compiler has my back. Since this blog post, I spent quite some time coding with Rust but, at some point, I always came to the same conclusion: the compiler is way too much on my back!
I know I should try to understand lifetimes and ownership and that once I will be mastering those topics my life as a Rust programmer will be perfect… and blah blah bah. But, for whatever reason, it doesn’t seem to work for me. And one day, while looking for a Web Framework, I came across Crystal Lang, and more especially, the Lucky Framework. That’s how it all began.
I’m a doer. I’ve been a doer of web stuff since 20 years now (😱) and I love when tools are helping me to achieve what I want, and that’s what Crystal Lang has to offer. It’s been a long time since I’ve enjoyed programming with a langage so much. Here is why:
- Strongly typed. You have a compiler that helps you and don’t get too much on your way.
- Familiar. It looks like Ruby. I’ve never done a lot of Ruby, but its syntax looks very natural.
- Easy to learn. Most of the concepts are familiar to everyone as it respects the “everything is an object”/OOP philosophy.
- Handles null value. You don’t have to worry about
Null Pointer Exceptionsand his friends.
What I do and don’t love
So for me, here are the main selling points of Crystal Lang:
- Has an useful compiler
- Handles null values
- Manage concurrency smoothly with fibers
- Feels like a dynamic language to write
- Has really good and mature web frameworks (Lucky, Kemal)
- Almost as fast as C
- Is easy to learn
- Has a welcoming friendly community
And here are the main difficulties (IMHO) with the language right now:
- Still young, so it’s missing a lot of libraries
- Small community. Even if it’s a friendly one, it’s hard to find people writing Crystal Lang code
Was it a show stopper for me? Not at all because :
- You already have a lot of awesome crystal libs available
- When you don’t, you just to find the Ruby one you would like to use and port it to Crystal (code is often the same)
- People are always happy to help
Here are some concrete examples:
- I wanted to parse markdown files coming from Nikola. I used the markd shard (that’s how libs are called in Crystal, shards) with some custom parsing.
- I wanted to highlight code on the backend in my posts. I plan to use noir a port of a Ruby Gem called Rouge, the one used for Jekyll. If it doesn’t support the language I want to highlight, I will just have to port the lexer from Rouge.
- I had troubles with my Lucky project regarding many2many relations. I asked a question on the Discord server, and the problem was solved very fast.
Should you give it a try? Definitively! It’s very fun to write Crystal Lang code. I’ve rewritten the engine of this blog in a few days from scratch and it was a real pleasure to do so.
Here are my feeling compared to other popular languages:
- Compared to Go: I really prefer the syntax of Crystal.
- Compared to Python: I love Crystal’s compiler (no, mypy is not quite the same)! I miss Python’s ecosystem and tooling.
- Compared to Elm: I wish I could write elm code on the backend!
- Compared to Elixir: Crystal’s compiler is so helpful. Even if fibers are great with Crystal, Elixir’s concurrency and reliability is way better (especially the actor system and let it crash philosophy).
You should give Crystal Lang a try if you want a language that helps you to write code that is fast and reliable. If you love the web, you should also give Lucky Framework a try. The documentation is very well done and people on the discord server are very (very) welcoming.