For a long time I've had the goal of finding a programming language to replace C in my general software development adventures, and my love-hate relationship with C has been something I've struggled with for quite a while, with my identity as a programmer, and as someone who genuinely enjoys programming, computer science, and the study of it.
Crystal in a way has given all of that back to me, and it almost feels magical :)
██████████████████
too_long.why_not_cC has always brought along with it this sense of immense dread, and in the programming community a reputation with it that is very much deserved. That being, when you begin to write C, you're signing a contract for an indefinite amount of time, to dedicate yourself to implement everything you need for your project yourself, refactor things multiple times due to core design flaws you overlooked the first time, and really become a part of the C programming cult... And it's just as much of a time sink as people say.
It might sound like I'm just complaining, but after extensive experience with using C and feeling "comfortable" with it for so long, it is an extreme demotivator for large projects, knowing a significant portion of it will end up meshed up in the process of just creating the architecture that your software will end up being built on. C is one of those programming languages which you spend your whole life trying to master, and end up about half way before you plateau. Honest people will admit that they never quite reach that level, even if they are extremely comfortable with it and active users of it in their projects they care about.
The honest reality of modern software engineering is that you don't really want to be doing all of the tedium that comes with *programming* itself. There is a lot of fun to be had in complex solving problems and building yourself a great platform to stand on as far as writing software goes, but most problems when working at the level of C are not complex, in fact they are extremely simple given you have the awareness to understand what went wrong in the first place. For most of us, we are software *engineers*, and playing this delicate cat and mouse game of "what did I do wrong this time?" is not part of the fun. We want to design software, and things like this get the real meat of designing software and seeing it come to fruition bogged down in the details of tracking down a memory corruption bug...
For that, most programmers^(and it's a lot more than you or I like to pretend it is) move on to something higher level, like C++, and Python. These programming languages advertise themselves directly to software engineers, the people who want to not just create, but build! And of course the specific approach that these programming languages take and their ecosystem leaves much to be desired, but it can't be overstated how much real value lives here. Out of dissatisfaction with C comes Go, and comes Rust, Zig, and many others that solve very specific problems with the developer ecosystem.
too_long.my_struggles_finding_a_replacementWe end up falling back to the basic question of "what's wrong with the other options?". In front of me are tens of different popular programming languages who in total fix the issues I have with C and using it for general software development, but yet I choose none of them, even as to burden myself with the whiplash of going back and forth between these very different styles of programming in trying out each one to figure out which I like.
I spent a great deal of time on and energy just this, I would find a new programming language that aligns with some goal of mine that I agree with on paper and I believe makes me more comfortable, however once I get in the weeds I discover it's really not what I'm looking for. By that point I've gotten much too attached to it, and this is really frustrating to me :(
I could go off on each one individually^(C++, Rust, Go, Zig, Odin, D, Ada, Hare) on my specific problems with them and how they don't fit my needs, but there's something unique here that doesn't get covered by all of them, which one cute little language does, and that's bringing back the joy of programming to me.
too_long.crystalCrystal scratches an itch I had no idea I had in the first place, and it comes from each of their main taglines that surprisingly hold true beyond a surface level. I think a part of this is the fact that the developers of the Crystal programming language use their own language for professional projects, not just personal ones.
Attached to this blog is a small program I'm writing to get used to the semantics of Crystal and gut a footing in it, and this has been the fastest I've created something like this in a long time! As a bit of history on me, I started programming with Lua, after considering Ruby and Java as a starting place. I started out with Java and was unable to grasp its tooling, and for Ruby I was unable to understand the code I wrote after leaving it alone for a bit. What Lua gave me that the others could not was a foundation of strong basic concepts that flow together like water, without resistance to eachother. Tables in Lua are everything and they are as much of a part of the code in Lua as how you think about the code itself and how it's structured.
This love for a simple yet strong set of tools is exactly what brought me to C in the first place, but Crystal has been the one to really bring it forth in this way :)
From the moment I started out with Crystal just a couple of days ago at the time of writing, the familiar but foreign syntax encapsulated me but kept me very curious, and just on the edge enough to not get so complelely comfortable that I lose sight of the end goal. I lost the love for a lot of programming languages like Elixir because they are deceptively simple and mask their complexity, they lead you into overconidence just like C does. Crystal is fundamentally different because, like Lua, it hooks the entire language on just a couple core features, which highlight how it achieves its fluid syntax and approachable reading/writing, through its standard library and by leaning heavily into OOP.
Crystal's amazing documentation leans into this beautifully by providing everything in one location that you can search for! There's a beginner overview which leans into its language reference, going over every feature of the language in distinct searchable sections, and the API reference, which covers both the standard library, and the language itself because Crystal embraces OOP in its design, while still remaining a statically typed language with low-level access. Crystal approaches this in a much different way than most would expect from other languages, but Crystal kind of *lies* to you...
too_long.crystal_lies_to_youIn a good way! Crystal has many features that feel like they lie to your face when you look into them, and it does this quite openly... Which is a different approach than other languages take. Zig for example prides itself and leads it's design explicitly by putting its foot down on things like this. Everything that the program does must be explicitly stated by the programmer in plain code, but Crystal does not have these standards whatsoever. Importing a module in Crystal has the potential to change how the addition operator behaves on an Int32 depending on the context within it's use, like using the colorizer module in Crystal modifies the behavior of String such that it adds a new method .colorize() which returns self, to allow method chaining, using symbols in place of enums to create something that Zig could never dream of!
record Day, short : String, long : String ... week_days = [ Day.new("Su","Sunday"), ... ] ... week_days.each do |day| ... print(day.short.colorize.fore(:black).back(:white)) ... end
This approach to language design is really eye opening because it changes the perspective of how you look at the language, and in a way that establishes a lot of trust. The compiler uses flow typing, which defers the type of a variable depending on the context around it, meaning you don't have to declare the type of a variable until it's relevant to *you* as a programmer. The compiler will even allocate a new variable with the same address when you assign it a new type! And a pointer to this new type will be both the old type and the new type... Depending on context
x = 0 p! typeof(x) # Int32 y = pointerof(x) x = "awawa" p! typeof(x) # String z = pointerof(x) pp! y # y # => Pointer(Int32 | String)@0x7ffeeff80c98 pp! z # z # => Pointer(Int32 | String)@0x7ffeeff80c98
Freaky... And the crazy thing is that the compiler will assert you understand which you're using in what situation, and requires you to assign a new local to disambiguize^(is that a word) which type is being operated on! This leads to Crystal naturally supporting optional types and default values, and that makes sense... But it just lingers in my head that the compiler is *lying* to me, but yet I feel comfortable with how it guides me. Almost like the Rust borrow checker, or Ada's strict typing, the language itself is verifying that my program is correct before it hits runtime, and that gives me a level of safety and confidence in my code that allows for rapid development, like a scripting language!
too_long.my_overall_thoughtsOverall I think that Crystal is a great language that happens to hit the perfect featureset for me to really enjoy developing things in it, and I hope to use Crystal to finally make some of the stuff I've been putting off for so long!
The thing I enjoy most out of these long winded sidequests to learn different programming languages is that I get to see different perspectives of the same basic fundamental programming concepts expressed in a new way! I say this a lot on this site, but learning is my favorite activity ever and I spend a lot of my time studying and reading about different topics, mostly related to computer science. There's something about getting hands on with things like this though that teaches me so much more than I could learn by just reading about it in a book, I get to see the intentions behind the creator, their values, and how it comes together into one cohesive being, and I always love seeing those things!
Crystal if nothing else is enamoring by its very existence. It's a very delicate blend of decades of history of high level scripting languages with the inevitable want to do so much more with limited tools that people who love scripting languages all feel. Whenever I would think in my head about what the perfect programming language might be for me, Crystal feels like a good faith implementation of most of those, especially for its flow typing and its object model :)