DEV Community

Aaron Schlesinger
Aaron Schlesinger

Posted on

3 2

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!

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more