6. Scoping Rules

The scoping rules that Flavor uses are identical with C++ and Java with the exception of parsable variables.

As in C++ and Java, a new scope is introduced with curly braces ({}). Since Flavor does not have functions or methods, a scope can either be the global one or a scope within a class declaration.

The global scope cannot contain any parsable variable, since it does not belong to any object. As a result, global variables can only be constants.

Within a class, all parsable variables are considered as class member variables, regardless of the scope they are encountered in. This is essential in order to allow conditional declarations of variables which will almost always require that the actual declarations occur within compound statements. Non-parsable variables that occur in the top-most class scope are also considered class member variables. The rest live within their individual scopes.

This distinction is important in order to understand which variables are accessible to a class variable that is contained in another class. The issues are illustrated in the following example.

    class A {
        int i=1;
        int(2) a;
        if (a==2) {
            int j=i;    
            int i=2; // hides i, ok
            int a; // hides a, error
        } 
    }
    
    class B {
        A a;
        a.j=1; // error, j not in a
        //class member
        int j=a.a+1; // ok
        j=a.i+2; // ok
        int(3) b;
    }

Looking at class A, the initial declaration of i occurs in the top-most class scope; as a result i is a class member. a is declared as a parsable variable, and hence it is automatically a class member variable. The declaration of j occurs in the scope enclosed by the if statement; as this is not the top-level scope, j is not a class member. The following declaration of i is acceptable; the original one is hidden within that scope. Finally, the declaration of the variable a as a non-parsable would hide the parsable version. As parsable variables do not obey scoping rules, this is not allowed (hiding parsable variables of a base class, however, is allowed).

Looking now at the declaration of class B which contains a variable of type A, it becomes clear which variables are available as class members.

In summary, the scoping rules have the following two special considerations. Parsable variables do not obey scoping rules and are always considered class members. Non-parsable variables obey the standard scoping rules and are considered class members only if the are at the top-level scope of the class.

Note that parameter type variables are considered as having the top-level scope of the class. Also, they are not allowed to hide the object identifier, if any.