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}
  render "src/views/index.ecr"
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)