Book Home Java Enterprise in a Nutshell Search this book

10.6. Naming Scopes

Each IDL file you create defines a namespace or naming scope for identifiers you declare within that file. This namespace is further subdivided into nested scopes whenever you declare a new module, interface, structure, union, or exception in your IDL file. You can think of the naming scope within an IDL file as a sort of naming directory. By default, you start at the root of the directory, and each time you open a declaration of one of these items, you start a new subdirectory of the naming directory, named after the identifier you use for the item.

You can specify a scope using the :: delimiter, which is analogous to the / or \ delimiter in file directories. The root scope for the IDL file is represented as :: by itself, and nested scopes are specified by adding their names, such as ::utils::math::MatrixOps. The names in a scope name can refer to any identifiers that might exist in each scope. In this example, utils and math might refer to modules (the math module is declared within the utils module), and MatrixOps might refer to an interface declared within the math module. The intermediate elements in a scoped name must refer to one of the IDL elements that define their own scopes, but the final element in a scoped name can refer to any item with its own identifier, including constants, data members on interfaces, etc.

Within any particular scope in the naming scope of an IDL file (including the root scope), all identifiers within that scope must be unique. Separate nested scopes off of one parent scope can have identical identifiers declared within them and can share identifiers with their parent scope as well, but two identifiers at the same level within a scope can't be the same. As as example, the following is legal in an IDL file:

// IDL
module utils {
	interface math {
		const float PI = 3.14159;
	};
	interface baking {
		const string PI = "apple";
	};
};

The two definitions of PI (::utils::math::PI and ::utils::baking::PI) do not conflict, since they each have distinct absolute scoped names within the IDL file. You cannot, however, declare a constant named math within the utils module, since its fully scoped name is ::utils::math, which conflicts with the name of the math interface.

Scoped names that begin with :: are absolute names, and are relative to the root file scope of the IDL file. Names that don't start with :: are relative to the local scope in which they appear. So we can add two new constants to our math interface that use scoped names to reference our versions of PI:

// IDL
module utils {
	interface math {
		const float PI = 3.14159;
		const float PIsquared = PI * PI;
		const string PIOfTheDay = ::utils::baking::PI;
	};
	interface baking {
		const string PI = "apple";
	};
};

The reference to PI in the definition of the PIsquared constant is relative to the ::utils::math scope, so it refers to the float constant. The reference to PI in the PIOfTheDay definition is absolute and references the string definition of PI in the baking interface.



Library Navigation Links

Copyright © 2001 O'Reilly & Associates. All rights reserved.