Anonymous code blocks in Groovy

Posted by piepera on Stack Overflow See other posts from Stack Overflow or by piepera
Published on 2010-04-19T20:52:00Z Indexed on 2010/04/19 20:53 UTC
Read the original article Hit count: 200

Filed under:
|

Is there a way to use anonymous code blocks in Groovy? For example, I'm trying to translate the following Java code into Groovy:

{
  int i = 0;
  System.out.println(i);
}
int i = 10;
System.out.println(i);

The closest translation I can come up with is the following:

boolean groovyIsLame = true;
if (groovyIsLame) {
  int i = 0;
  System.out.println(i);
}
int i = 10;
System.out.println(i);

I know anonymous code blocks are often kind of an antipattern. But having variables with names like "inputStream0" and "inputStream1" is an antipattern too, so for this code I'm working on, anonymous code blocks would be helpful.

© Stack Overflow or respective owner

Related posts about groovy

Related posts about java