Names and Scoping

4 min read

4 min read

4 min read

4 min read

4 min read

4 min read

4 min read

4 min read

4 min read

The Slice compiler resolves an identifier in the scope where you use it. This page gives the rules for declaring identifiers in a scope and for referring to them.

The global scope, modules, interfaces, classes, structs, exceptions, enumerations, and the parameter list of an operation each form a naming scope. Within a scope, each identifier must be unique, and identifiers that differ only in capitalization are the same identifier; see Case Sensitivity. Reopening a module and defining a forward-declared class or interface do not count as redefinitions.

Slice
interface Bad
{
void op(int value, string value); // Error: duplicate parameter name
}

The scope-qualification operator :: refers to a definition in another scope:

Slice
module Types
{
sequence<long> LongSeq;
}
module MyApp
{
sequence<Types::LongSeq> NumberTree;
}

Types::LongSeq refers to the LongSeq sequence of module Types. The compiler resolves a name that starts with :: from the global scope: ::Types::LongSeq is the fully qualified name of this sequence.

An identifier defined in an inner scope hides the same identifier in an enclosing scope. A qualified name still refers to the hidden definition:

Slice
module Outer
{
sequence<string> Seq;
module Inner
{
sequence<short> Seq;
struct Example
{
Seq a; // Sequence of short
::Outer::Seq b; // Sequence of string
}
}
}

An operation cannot have the name of its enclosing interface, whatever its capitalization.

To resolve a name that does not start with ::, the compiler searches the definitions that precede it in the current scope, then each enclosing scope in turn, up to the global scope. It never takes a name from a base interface or class:

Slice
module M
{
sequence<double> Seq;
module A
{
sequence<string> Seq;
interface Base
{
Seq getBase(); // Sequence of string
}
}
module B
{
interface Derived extends A::Base
{
Seq getDerived(); // Sequence of double
}
}
}

In A::Base, Seq is M::A::Seq. In B::Derived, it is M::Seq, even though Derived extends A::Base.

A scope cannot hide an identifier it has already used. A qualified name such as M::Seq uses only M, and a fully qualified name such as ::M::Seq uses no identifier of the current scope:

Slice
module M
{
sequence<string> Seq;
interface Bad
{
Seq op1();
int Seq(); // Error: Seq has changed meaning
}
interface Good
{
::M::Seq op1();
int Seq(); // OK
}
}

In Bad, op1 uses Seq, so an operation named Seq cannot hide it. In Good, ::M::Seq uses no identifier of Good, so Seq can name an operation.

A parameter can reuse an identifier from an enclosing scope, such as the name of its operation or of a type:

Slice
module M
{
sequence<string> Seq;
interface I
{
string query(string query); // OK
void op(Seq Seq); // OK
}
}

A field can likewise reuse an identifier from an enclosing scope.