Skip to content

Instantly share code, notes, and snippets.

@anicholson
Created April 19, 2020 22:45
Show Gist options
  • Save anicholson/714503c05584225a55d478c04a48adfe to your computer and use it in GitHub Desktop.
Save anicholson/714503c05584225a55d478c04a48adfe to your computer and use it in GitHub Desktop.
Crystal macro generates incorrect syntax?
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: ,
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
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