Instance variables initialized within {% begin %} {% end %} does not compile.
https://play.crystal-lang.org/#/r/6wbt
struct Foo
@value : Int32
def initialize
{% begin %}
@value = 5
{% end %}
end
end
Error in line 2: instance variable '@value' of Foo was not initialized directly in all of the 'initialize' methods, rendering it nilable. Indirect initialization is not supported.
Not a bug, macros are analyzed on method instantiation. There's a similar bug report (which is not a bug to me) if you search something like initialize macro.
It looks like initialization in a macro is not possible without {{@type}}. So this will work:
struct Foo
@value : Int32
def initialize
{{@type}}
{% if true %}
@value = 5
{% end %}
end
end