8
votes
Experience with Crystal programming language?
I have heard just a little bit about the language Crystal every so often, probably since it was first mentioned on /r/programming. From what I know, it's Ruby-like syntax but with a static type system, which seems like a big benefit to me.
I written just a little bit of Ruby, so the syntax isn't very familiar to me, and I haven't bothered trying Crystal out, but I'm curious to know what kinds of things people do with it.
So, my questions are: Do you have any experience with Crystal? If so, what have you used it for? Was it a professional or recreational project? How did you like it? What about it stood out to you compared to your experiences with other languages?
Thanks!
I've used Crystal a ton over the years!
For context, I got my start in java, then moved to web development while doing stuff in C (arduino) at the same time. I also work in python a lot as part of university.
Crystal has nailed a LOT of my biggest criteria, and I highly recommend it. I'll enumerate a few:
Speed: Crystal isn't going to outperform C or Rust unless you're really forcing it to, but it does not disappoint in terms of speed. I do a lot of scientific computing for my degree, and Crystal lets me be lazy and write dumb, unoptimized code that still only takes a couple seconds to run. I do usually write optimized code anyway, but it's nice to have that be a choice and not a requirement.
Typing: Crystal's type system is really neat - I won't explain it here, but long story short: specifying types is optional, but the compiler will figure out all types your program uses and ensures that the code you've written makes sense. So if you have a function sub(a, b) that returns a-b, you don't have to specify what types a and b are. But, if you call the function with a string and float, the program will not compile. This is what lets crystal look and feel like an interpreted language, while still being so fast and type safe.
Standard library: This is hugely important, in my opinion. I have never seen a standard library as well curated and generally useful as Crystal's. Regex? Built in, easy. Parsing config files? In there. Crystal also has incredible serialization support, so saving and loading objects to disk is easy. The standard library is also really well factored - for example, if you need a custom container type, you can just include the "Indexable" module in your container, and suddenly it can do everything a normal array can.
Package management: Crystal's package manager is basically the only one I can tolerate. You tell it where to find the package you want, and then it clones the library into your project's "lib" folder. Packages are only installed locally, you don't need to worry about venvs or global version conflicts. It is the only package manager that feels like it makes things easier, not more convoluted.
Build System: There isn't one :) Almost all programs are built and run in the exact same way, and you don't have to spend hours digging through cmake documentation before getting someone's code to run.
Metaprogramming: Crystal has a very powerful macro system. I once wrote breadth first search in it as part of a compile-time safety check I wanted to add to a weird function. The documentation for this is lacking, though.
Syntax: This is the most subjective, but ruby (and hence crystal) is one of the prettiest languages I've seen. There aren't any useless decorators (semicolons are an example, although I used to prefer them, a better example is brackets around the condition of an if statement), the style is very functional (you can do things like array.each to iterate, array.each_with_index if you need an index as well), and boilerplate is nonexistent. It also uses a more ergonomic set of brackets than most languages - parentheses are used for generic types rather than angle brackets, for example.
Apologies for the erratic writing - I'm exhausted, but I figure it's the least I can do to share some of my favourite things about the language.
Oh man, you have reminded me of this long abandoned personal project that I got stuck on exactly this problem. I should really get back to it.
Us programmers are really unkind to ourselves and our peers, huh? Haha
I spent the last two days diving through broken ass CMake to figure out why I could configure in one version of the software, but not the next. I know this feel :)
Eh, it’s mostly my fault for thinking, “C++ must be just like Java,” and delving into a mature codebase.
I’ve even talked to the dev a few times and while he is nice I would not like to be the newbie who asks him fundamental questions when it’s clear it’s not a project I was prepared to get involved in.
Thanks for the great answer! This makes me want to give Crystal a try on a future project. In your experience, what kinds of use cases is Crystal best suited to? That is, what kinds of projects would be best to get acquainted with it? For example, I know Ruby is often used for web programming with Ruby on Rails; is there a similar framework available for Crystal?
Crystal has a few web development frameworks, but I have not used them myself. There’s a collection of most crystal packages (shards) here, and the web framework section of that page is probably helpful. I’m somewhat friends with the author of the Athena library, so it might be worth checking out :)
I’m not really aware of any areas where Crystal can’t reasonably be used. Personally, I’ve been drinking a little bit of the Rust kool-aid recently, so I think that things like GUIs and embedded devices should avoid languages that allow direct use of pointers and garbage collection (Crystal is an example of this, but so are almost all modern languages - this is a pro-rust point, not an anti-crystal one).
It’s worth mentioning that the community is still small, so there are some rough edges to be found. None of them are unfixable, and they will only get better with time. However, if you’re brand new to programming, or you’re looking to learn a language that will land you a tech job, Crystal is not the right choice right now.
I also want to mention that, although Crystal is a compiled language, there is actually an interpreter being developed by the core team. So, at some point in the next couple of years, you’ll be able to use Crystal just as dynamically as ruby or python, and then compile it for a free 80x speedup :)
Feel free to ask me any other questions you have.
I wrote a little bit of recreational code in Crystal, nothing big.
To me it felt like Ruby with classes and a little bit of golang (fibers and channels gave me really strong golang-like vibes). It was easy to get into and fun to write. Unfortunately I have no experience in maintaining bigger projects in it over a longer period of time, though.
I use Ruby professionally. It does what I expect it to, and meets my needs, so I haven't needed to try things like Crystal.