DEV Community 👩‍💻👨‍💻

Aaron Schlesinger
Aaron Schlesinger

Posted on

Quick thought on Crystal

So I just realized something about the Crystal language...

It has the most "magic" I've seen in a compiled language. Usually magic features in programming languages show up in scripting languages. I'm talking about things like monkey patching and I usually get too fancy and dig myself a massive hole with that kind of stuff.

Here's some Crystal magic I just discovered in my live stream:

get "/" do
  key_value_pairs = [] of {String, String}
  db.each do |key, value|
    key_value_pairs << {key, value}
  end
  render "src/views/index.ecr"
end
Enter fullscreen mode Exit fullscreen mode

That renders a template, and makes sure that key_value_pairs is available in it. Most languages can do that, so it's nothing special.

Crystal's magic is that if you try and use key_value_pairs wrong - like as a HashMap - the compiler will yell. If you try to use a variable that you didn't pass to the template, the compiler will yell as well. Magic!

If you're, this magic was brought to you by macros

Mad props for fitting this kind of magic into a compiled language!

Top comments (0)

Hacktoberfest is happening now!



It is a month-long celebration of open source. For a lot of devs, its their introduction to open source.


Check out the Hacktoberfest tag on DEV to keep up with the latest!