Created
April 19, 2020 22:45
-
-
Save anicholson/714503c05584225a55d478c04a48adfe to your computer and use it in GitHub Desktop.
Crystal macro generates incorrect syntax?
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
There was a problem expanding macro 'sequential' | |
Code in src/minimal_example.cr:8:1 | |
8 | sequential do | |
^ | |
Called macro defined in src/minimal_example.cr:1:1 | |
1 | macro sequential(&block) | |
Which expanded to: | |
1 | | |
2 | | |
> 3 | sequence << "first", sequence << "second", sequence << "third", puts(sequence) | |
^ | |
Error: unexpected token: , |
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 sequential(&block) | |
{% expressions = block.body.is_a?(Expressions) ? block.body.expressions : [block.body] %} | |
{{ *expressions.select { |x| (x.class_name == "Call") }.map { |x| x.block.body } }} | |
end | |
sequence = [] of String | |
sequential do | |
first { sequence << "first" } | |
second { sequence << "second" } | |
third { sequence << "third" } | |
last { puts sequence } | |
end |
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
sequence << "first" | |
sequence << "second" | |
sequence << "third" | |
puts sequence | |
# or even | |
sequence << "first";sequence << "second";sequence << "third";puts sequence |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment