
I would argue that 99% of the time, your dynamic method definition could be replaced with a compile-time macro in as many lines of code. Many people are afraid to use Crystal because they can’t dynamically define methods as it is a compiled language, but macros let you do just that at compile time.
#CRYSTAL PROGRAMMING LANGUAGE PDF CODE#
Crystal lets you leverage a large percentage of the language when writing macros, so you can do crazy powerful things that normally would be impossible in a statically compiled language like having a library class written by person A reason statically about and change its code based on the contents of an implementer class written by person B at compile-time. In C/C++ land it is extremely difficult to do things statically without writing some confusing macros using very antiquated syntax. Even better, the Crystal compiler only requires you to explicitly specify types when it is syntactically ambiguous what the type of a variable should be, meaning that often you don’t even need to deal with types directly and can proceed like you normally would in Ruby/Python-land. By enforcing static typing, Crystal rules out a huge number of type related bugs and paves the way for optimizations and static compilation. Ruby/Python are dynamically typed, so it is very difficult to reason about and optimize Ruby/Python code. Crystal is a statically typed language, meaning every variable has one or more potential types and these need to be figured out at compile time and accounted for. To avoid getting into a huge discussion about this, a truly complete fibers implementation like the one in Crystal lets you get around callback/promise hell (JavaScript) and makes it a lot easier to write things like game/UI loops, events, asynchronous and synchronous IO, etc.
#CRYSTAL PROGRAMMING LANGUAGE PDF FOR FREE#
One thing you don’t get for free in the C/C++ world is Fibers. For this reason, Crystal is sometimes advertised as similar to Rust but easier to read and write. Like many interpreted languages, Crystal will let you do pretty much whatever you want in as few lines of code as possible, and in a way that is readable and aesthetically pleasing.

Just look at some of the latest benchmarks:

As I mention earlier, Crystal is a statically compiled language based on the venerable LLVM framework, and will go toe-to-toe with C/C++ and Rust any day of the week. And did I mention your code will be beautiful and you can still use dynamic methods sort of? Why Crystal? 11 Reasons With Crystal, however, you can do all the stuff you were doing in Ruby/Python but with the speed and memory footprint of a native C/C++ binary. Since it is much easier to develop an MVP in Ruby/Python than it is in C/C++, for the past 18+ years SAAS companies large and small have simply accepted dynamic languages as a necessary evil and have taken on these extra performance hits as an unavoidable cost of doing business. In other words, we have always had to choose between performance and syntax. Until now, you always had the choice between writing a lot of hard-to-read, hard-to-debug, complicated, but extremely fast C/C++ code, and writing a short 5-liner in Ruby/Python that does what you want, but wastes memory and speed. The main draw of Crystal is that it appears to have shattered the barrier between syntactically sweet interpreted/dynamic languages like Ruby and Python - which are loved for their readability and ease-of-use - and the raw horsepower of C/C++ and low-level systems languages.

No more trade-offs: “Fast like C, slick like Ruby”

In the following article I will make the case for why Crystal - rather than the likes of JavaScript, Elixir, Rust, Ruby, Python, or Go - is the most exciting and promising programming language in recent history.
