Chapter 9: Macros and Metaprogramming
We love Ruby because of its’ dynamic nature and metaprogramming! Unlike Ruby, Crystal is a compiled language. That’s why there are some key differences.
There’s no
eval
.There’s no
send
.
In Crystal we use Macro
s to achieve this kind of behaviour and metaprogramming. You can think of Macro
s as ‘Code that writes/modifies code’.
P.S: Macro
s are expanded into code at compile-time.
Check this.
In the example we created a macro named define_method
and we just called that macro like a normal method. That macro expanded into
Pretty cool! We got eval
behaviour at compile-time.
Macros are really powerful but there’s one rule that you can’t break.
A macro should expand into a valid Crystal program
Last updated