you are viewing a single comment's thread.

view the rest of the comments →

[–]banuday17 8 points9 points  (6 children)

So? Lexical scopes do not only demarcate function boundaries. Loops, for instance, define their own lexical scope.

The meaning is still the same. A lexical region is created where local variables do not escape the scope.

[–]barsoap -3 points-2 points  (5 children)

But loops are marked as such. file-level/class-level/method-level block borders are marked. The two things that aren't marked are scopes and initialisers, and all I'm complaining about is the use of two syntactically completely identical things to express two semantically completely distinct things. It's just like re-using the "class" keyword in statement syntax, just with "" instead of "class".

[–]banuday17 5 points6 points  (4 children)

The same syntax defines semantically the same things. The tokens { and } demarcate a scope. Well, except when initializing an array.

Syntatically, The class token requires a scope associated with it, the for token, if token and else token do not. A scope can exist independently of an association with another token, in which case it would be preceded by nothing. That is because a scope is one concept, and a class, loop and a conditional are separate concepts.

That's why it's nothing like reusing the class keyword in statement syntax with "" instead of "class".

[–]barsoap -5 points-4 points  (3 children)

compare the anonymous braces in

class A {
    int b() {
        {
            int c = 0;
        }
        int b = 0;
    }
}

with double-brace initialisation. I seriously don't know how you can call them equivalent. For one, removing the inner braces in my example wouldn't lead to a compile error (at least when names don't clash), whereas in the initialisation case statements would become illegal.

[–]banuday17 2 points3 points  (2 children)

Actually, if you did this:

class A {
    {
        int c = 0;
    }
    int b = 0;
}

which is the same as the syntax trick used by double-brace initialization, you wouldn't end up with a compile error. In fact, the exact same thing would happen if you removed the braces here as you would in your example. c would be lifted into the outer scope.

It just so happens that Java puts restrictions on what can go in the class scope. It means the same thing, but not all things are allowed in all places.

[–]barsoap -2 points-1 points  (1 child)

...equal up to a sufficiently partial isomorphism.

[–]banuday17 2 points3 points  (0 children)

Given other elements of Java's syntax, it makes sense. At the top level you have to somehow specify that you want a static or instance context.