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.
Eg:
| Code Block |
|---|
do{ i++; return c }if(
( severance.taxAmount >= TaxYear2006.taxAmounts.severanceLimit[ 'dependent' ] ||
... ... ...
... ... ...
)
|
is transformed to:
| Code Block |
|---|
if( ... ... ... ){ i++; return c }
|
We could use 'unless' and 'until' if using 'if' and 'while' caused problems.
Common modifier
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:
| Code Block |
|---|
static{
def a(){ ... }
def b(){ ... }
}
|
becomes
| Code Block |
|---|
static a(){ ... }
static b(){ ... }
|
Transform statements
Eg:
| Code Block |
|---|
def genUniqueScriptName(){ static int i=1; 'Script_' + i++ }
|
becomes
| Code Block |
|---|
class UniqueScriptName{ static int i=1; static gen(){ 'Script_' + i++ }
|
triggered by the 'static' modifier of a field in a function.
Names for operators
Use names instead of symbols for operators, eg:
| Code Block |
|---|
if( a and b ) |
becomes
| Code Block |
|---|
if(a && b) |
or change their names to something shorter, eg:
| Code Block |
|---|
if( a of A ) |
becomes
| Code Block |
|---|
if( a instanceof A ) |
Equivalence of block and statement
Forgoing curlies for all statements, making a statement and block equivalent always:
Eg:
| Code Block |
|---|
try A.call() catch(e) if(e instanceof ABCException) throw new BusinessException() |
Use other natural languages with Groovy
Enable fuller internationalization of Groovy, eg:
| Code Block |
|---|
mientras( i < 10 ){ ... ... ... }
|
would convert to
| Code Block |
|---|
while( i < 10 ){ ... ... ... }
|
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).
eg:
| Code Block |
|---|
int i= 0
java('jdk1.5.0_07'){ System.out.println i; class ... ... ... }
|
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:
| Code Block |
|---|
String reading= 'OK |
expands to:
| Code Block |
|---|
private String reading= 'OK'
public String getReading(){ this.xxxx }
public void setReading( String s ){ this.xxxx= s }
|
Other examples:
- for( i in ... ) expanding to code using iterator
- shorthand notation for invoking method with closure/s at last parameter/s