Use this page to list use cases for AST-level macros in Groovy.
How do you want to use such macros? Show examples of their usage (without showing how they're implemented). Add to the bottom of the page.
Increase code readability
Rearrange code portions
The end-weight principle for natural languages says the longer stuff should be at the end of a sentence.
In Groovy/Java, while and if statements are usually heavy in the block/statement, and light in the condition.
When the statement/block is short and the condition long, it can be more readable to put the block last.
is transformed to:
We could use 'unless' and 'until' if using 'if' and 'while' caused problems.
When a long list of methods and/or fields have the same modifier/s,
it may be more readable to apply them to a whole block, eg:
triggered by the 'static' modifier of a field in a function.
Names for operators
Use names instead of symbols for operators, eg:
or change their names to something shorter, eg:
Equivalence of block and statement
Forgoing curlies for all statements, making a statement and block equivalent always:
Use other natural languages with Groovy
Enable fuller internationalization of Groovy, eg:
would convert to
if a Spanish option for Groovy was loaded.
More Seamless Integration with Java and Java-like languages
Embed Java code within Groovy without quoting it,
and without needing to bind variables to the same name, ie, no binding.setVariable('i', i).
Simplify the Antlr lexer/parser
Many existing syntactic sugars could be re-implemented as macros in a standard macro library.
This might simplify the Antlr lexer/parser, enabling better maintenance of and extensions to it.
Eg, properties where:
- for( i in ... ) expanding to code using iterator
- shorthand notation for invoking method with closure/s at last parameter/s