19.11.2014 Views

The Fortress Language Specification - CiteSeerX

The Fortress Language Specification - CiteSeerX

The Fortress Language Specification - CiteSeerX

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

We first define a declaration’s reach. <strong>The</strong> reach of a labeled block is the block itself. A method declaration not in<br />

an object expression or declaration must be in the declaration of some trait T , and its reach is the declaration of T<br />

and any trait or object declarations or object expressions that extend T ; that is, if the declaration of trait T contains a<br />

method declaration, and trait S extends trait T , then the reach of that method declaration includes the declaration of<br />

trait S. <strong>The</strong> reach of any other declaration is the smallest block strictly containing that declaration (i.e., not just the<br />

declaration itself). For example, the reach of a top-level declaration is the component containing that declaration, the<br />

reach of a field declaration is the enclosing object declaration or expression, the reach of a parameter declaration is<br />

the functional declaration or function expression in whose parameter list it occurs, and the reach of a local variable<br />

declaration is the smallest block in which that declaration occurs. We say that a declaration reaches any point within<br />

its reach.<br />

It is a static error for two declarations with overlapping reaches to declare the same name other than self (even if the<br />

name is declared in different namespaces) unless one of the following conditions holds:<br />

• both declarations are functional declarations with the same reach,<br />

• both declarations are method declarations that occur in different trait declarations,<br />

• one declaration is a field or keyword-parameter declaration whose reach is strictly contained in the reach of the<br />

other declaration, or<br />

• one declaration is a method declaration that is provided by (i.e., occurs in or is inherited by) some trait or object<br />

declaration or object expression that is strictly contained in the reach of the other declaration.<br />

If either of the first two conditions holds, or if one declaration is a field or method declaration that occurs in an object<br />

declaration or expression that inherits the other declaration (which therefore must be a method declaration), then the<br />

two declarations are overloaded, and subject to the restrictions on overloading (see Chapter 33).<br />

If two declarations with overlapping reaches declare the same name in the same namespace, and the declarations are<br />

not overloaded, then at any point that their reaches overlap, one declaration shadows the other for that name in that<br />

namespace; we may omit the name and namespace when it is clear from context. Shadowing is permitted only in the<br />

following cases:<br />

• In a trait or object declaration, any declaration in a block enclosing the declaration is shadowed if it declares a<br />

name of a field or method provided (declared or inherited) by the trait or object being declared.<br />

• In a method declaration that does not give an explicit name other than self for the self parameter, any declaration<br />

of self (including implicit declarations) in a block enclosing the field or method declaration is shadowed.<br />

• In the ensures clause of a contract, any declaration of result in a block enclosing the ensures clause is<br />

shadowed.<br />

• In a function or method declaration with keyword parameters, any declaration in a block enclosing the declaration<br />

is shadowed if it declares the name of any of the keyword parameters.<br />

We say that a name is in scope in a namespace at any point in the program within the reach of a declaration that<br />

declares that name in that namespace unless one of the following conditions holds:<br />

• the declaration is shadowed for the name in that namespace,<br />

• the declaration is a variable declaration, the namespace is the value namespace, and the program point is in<br />

the initial-value expression of the declaration or an initial-value expression of another declaration that is in the<br />

smallest lexical block enclosing the declaration and lexically precedes the declaration in that block.<br />

Note that the last condition applies to the method names declared by a wrapped field declaration.<br />

We say that the scope of a declaration for a name in a namespace consists of those points at which the name is in scope<br />

for the namespace and the declaration is not shadowed for that name and that namespace. Again, when it is clear from<br />

context, we may omit the name and namespace.<br />

62

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!