views:

2507

answers:

9

for example, I have the following code (generated, not written)

  if(node.getId() != null)
    {
        node.getId().apply(this);
    }
    {
        List<PExp> copy = new ArrayList<PExp>(node.getArgs());
        for(PExp e : copy)
        {
            e.apply(this);
        }
    }
    outAMethodExp(node);

What do those extra curly braces mean?

+2  A: 

They make an inner scope. Variable declared inside these braces is not visible outside of them. This also applies to C/C++.

phjr
+1  A: 

i think they just define an unnamed level of scope

A: 

They define a new scope which means that everything declared in this scope is not visible outside the curly braces.

fhe
+36  A: 

The only purpose of the extra braces is to provide scope-limit. The List<PExp> copy will only exist within those braces, and will have no scope outside of them.

If this is generated code, I assume the code-generator does this so it can insert some code (such as this) without having to worry about how many times it has inserted a List<PExp> copy and without having to worry about possibly renaming the variables if this snippet is inserted into the same method more than once.

matt b
A: 

I'd actually guess that someone forgot an else statement...

EDIT:

Jesus Christ you people can be dense. There's rarely a good reason to even bother with creating additional block scopes. In this, and most cases, it's far more likely someone may have forgotten to type their control statement than it is that they were doing something clever.

Patrick
I'm voting you up simply because it can happen, has happened, and Occam is rolling in his grave because you were voted down. :)
Abyss Knight
It is generated by SableCC. I bet 5$ they did not forget else
Pavel Feldman
I'm upvoting you for the same reason as Abyss.
Chris Lively
+1  A: 

The bring a scope, copy will not be visible outside of it, so you can declare another variable with same name later. And can be gathered by garbage collector right after you exit that scope. In this case copy serves as a temporary variable, so it is a good example.

Pavel Feldman
A: 

As an interesting note: the braces actually enable a class of statements: declarations.

This is illegal: if(a) int f;

but this is legal: if(a) { int f; }

Hugo
Exactly this code is useless, you will not be able to see f outside of braces. And to use it you need to declare and use it inside same scope. So you need more than one statement, so you need braces.
Pavel Feldman
that's the point of the compiler check that i'm mentioning. I find it interesting that there is a difference in "implicit scope" (which i consider each one-liner without braces to be) and explicit ones. It's easy to forget that the compiler would make a difference.
Hugo
+2  A: 

I second what matt b wrote, and I'll add that another use I've seen of anonymous braces is to declare an implicit constructor in anonymous classes. For example:

  List<String> names = new ArrayList<String>() {
    // I want to initialize this ArrayList instace in-line,
    // but I can't define a constructor for an anonymous class:
      {
        add("Adam");
        add("Eve");
      }

  };

Some unit-testing frameworks have taken this syntax to another level, which does allow some slick things which look totally uncompilable to work. Since they look unfamiliar, I am not such a big fan myself, but it is worthwhile to at least recognize what is going on if you run across this use.

Dov Wasserman
I am sorry, I don't understand this code. Is "add" a class or a function. If it is a function: what class does it belong to? Is ArrayList accepting a delegate type in this case?
Peter Mortensen
+2  A: 

I agree with the scope limit answer, but would add one thing.

Sometimes you see a construct like that in the code of people who like to fold sections of their code and have editors that will fold braces automatically. They use it to fold up their code in logical sections that don't fall into a function, class, loop, etc. that would usually be folded up.

Eli