Anvil | Smiths Smiths | Register Register | Login Login |
Search:
Show links Show tools Show tree | Previous document Next document | njet.org > Anvil > Documentation > Language reference > Scopes and entities

Scopes

Anvil uses static binding, whenever possible, but as typing is performed at a runtime, this cannot always be achieved. Binding means that names or dotted names (e.g. com.acme.tool) are attached to entity they are referring to. For instance, when refering to IO library, anvil.io is used. When static binding cannot be achieved then dynamic binding is used; this happens at a runtime. For instance, when method if invoked on an object: data.toString().

Entity is one of the language constructs:

  • module
  • namespace
  • class
  • interface
  • import
  • library
  • function
  • method
  • interface method
  • constructor
  • constant variable
  • module variable
  • member variable
  • parameter
  • local variable.

Entities are declared in scopes. Within one scope the names of entities cannot be same, even if these entities were of different type. Scopes that can be declared on scripts are:

Module

Scope of module, can contain constants, variables, functions, classes, interfaces and namespaces. To refer to module explictly, module or its name is used.

Namespace

Scope of namespace can contain constants, variables, functions, classes, interfaces and namespaces.

Class scope

Scope of class, can contain member variables, variables, constants, functions, methods and classes. To refer to class, class can be used inside the class and name of class from elsewhere. Entities from inherited classes and from implemented interfaces are also visible.

Interface scope

Scope of interface, can contain constants, variables, functions, interface methods and interfaces. Entities from inherited interfaces are also visible.

Callable scope

Scope of function/method, can contain local variables, parameters and functions/methods. However, these entities are not visible outside functions, although nested functions may access the entities declared on parent function(s).

Imported scopes

Other scopes (or interfaces) may be imported to any scope, using import statement. These entities appear as if they would have been declared in the scope that imported them, althought the imports are NOT visible outside current module.

Server namespaces

Server configuration allows declaration of namespaces. These are referrable with their given name.

When binding for name is searched for, the search always starts from current scope and continues downwards until an entity is found. If all other scopes fail, then the last scope for searching is always anvil.lang module. Unconditional error is generated for name that couldn't be bound.

Once an anchor is found, i.e. an entity that matched the first part of dotted name, the search recurses into that entity matching as much from the dotted name as possible.

See also  | Language reference  | namespace  | Classes  | Interfaces  | Type operators  | Grammar
Contributes notes:
Add a note
What's new | Anvil