Skip to content

Instantly share code, notes, and snippets.

@Sija
Created July 22, 2024 13:28
Show Gist options
  • Save Sija/727a7fe00fba7a4c8291f9a2c75cb092 to your computer and use it in GitHub Desktop.
Save Sija/727a7fe00fba7a4c8291f9a2c75cb092 to your computer and use it in GitHub Desktop.
`guard` macro for Crystal language
macro guard(*exprs, &block)
{% if exprs.size == 1 %}
({{ exprs.first }}).tap do |v|
{{ yield }} unless v
end
{% else %}
{
{% for expr in exprs %}
({{ expr }}),
{% end %}
}.tap do |conditions|
{{ yield }} unless conditions.all?
end
{% end %}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment