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.
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:
do{ i++; return c }if(
( severance.taxAmount >= TaxYear2006.taxAmounts.severanceLimit[ 'dependent' ] ||
... ... ...
... ... ...
)
|
is transformed to:
if( ... ... ... ){ i++; return c }
|
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:
static{
def a(){ ... }
def b(){ ... }
}
|
becomes
static a(){ ... }
static b(){ ... }
|
Eg:
def genUniqueScriptName(){ static int i=1; 'Script_' + i++ }
|
becomes
class UniqueScriptName{ static int i=1; static gen(){ 'Script_' + i++ }
|
triggered by the 'static' modifier of a field in a function.
Use names instead of symbols for operators, eg:
if( a and b ) |
becomes
if(a && b) |
or change their names to something shorter, eg:
if( a of A ) |
becomes
if( a instanceof A ) |
Forgoing curlies for all statements, making a statement and block equivalent always:
Eg:
try A.call() catch(e) if(e instanceof ABCException) throw new BusinessException() |
Enable fuller internationalization of Groovy, eg:
mientras( i < 10 ){ ... ... ... }
|
would convert to
while( i < 10 ){ ... ... ... }
|
if a Spanish option for Groovy was loaded.
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:
int i= 0
java('jdk1.5.0_07'){ System.out.println i; class ... ... ... }
|
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:
String reading= 'OK |
expands to:
private String reading= 'OK'
public String getReading(){ this.xxxx }
public void setReading( String s ){ this.xxxx= s }
|
Other examples: