Created
July 22, 2024 13:28
-
-
Save Sija/727a7fe00fba7a4c8291f9a2c75cb092 to your computer and use it in GitHub Desktop.
`guard` macro for Crystal language
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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