Open syalon opened 1 week ago
You probably want to use a macro here:
macro assert(cond, &)
{% if flag?(:debug) %}
raise {{yield}} unless {{cond}}
{% end %}
end
Related: https://github.com/crystal-lang/crystal/issues/4263
Otherwise this is essentially a duplicate of https://github.com/crystal-lang/crystal/issues/7975
This is not exacty #7975. The variable is not declared within the macro. It's only used there.
But it seems to be a similar issue. The compiler determines whether a block argument is a caputered block depending on whether the variable is referenced in the method body. Apparently it cannot recognise it when inside a macro. So this relates to #8764.
It's easy to work around by inserting an explicit reference to the block variable outside the macro.
def assert(cond, &err : -> String)
err
{% if flag?(:debug) %}
raise err.call unless cond
{% end %}
end
my test code
compile result
try online: https://play.crystal-lang.org/#/r/gxy9