Namespaces

Revision as of 17:00, 1 May 2024 by Lchrisman (talk | contribs) (Removed redirect to Internal:Namespaces)


New to Analytica 6.5, and still experimental, not fully exposed, unsupported and subject to change.

This page describes a feature that is only partially implemented (as of Analytica 6.5 beta). Despite being incomplete, it is in a state where it is useful for some purposes, including Lumina-internal development, and hence is documented here. It can be extremely useful, but may be subject to change between now and feature completion.

What is a Namespace?

A global identifier is a name for a global object. Each global object belongs to a namespace and within that namespace, its identifier must be unique. For example, you cannot have two objects both named X in the same namespace. In fact, if you try to create a second object named X in the same namespace, Analytica will change it to be unique by adding numbers to the end, for example by changing the new name to X1. However, you can create a second object named X in a distinct namespace,

An object with a module or library class or subclass can be configured to be a namespace. An object of one of these classes is not namespaces normally a separate namespace, so it must be explicitly configured to be so. Filed modules and libraries are automatically configured to be namespaces, and an object with class Model is automatically a namespace.

Internally, the presence of the NamespaceDefaultScope attribute on a module subclass is what actually determines whether a module object is a namespace. Modules or libraries without this attribute are not namespaces, whereas those with this attribute are. The attribute value must be either Public or Private, with the meaning of this value described below.

In addition to your model's namespace(s), there is also a system (or root) namespace which holds the built-in functions, system variables, etc. Analytica will usually add numbers to the end of your identifier it collides with an identifier in the system namespace, but in theory that CAN co-exist. When a system object is added to Analytica in a new release, collisions with existing legacy models do arise, and in this case Analytica allows both to have the same name.

Qualified Names

When there are multiple identifiers with the same name, each in a different namespace, you may need to disambiguate which object you want by using a qualified namein the form NS :: X. For example, suppose NS1 and NS2 are names of two distinct namespaces, each containing a different object named X. Then you could use NS1 :: X or NS2 :: X to disambiguate which object you want.

Namespace names are themselves identifiers and thus subject to the same properties of other identifiers -- i.e., two namespaces might have the same name when each is in a distinct namespace. Thus, you might use a qualified name to get to the namespace of interest, resulting in a namespace path such as: NS1 :: NS2 :: X.

The operator :: is called the namespace operator. It has the highest precedence and cannot be broken up by parentheses. It must contain an identifier, qualified identifier or nothing on its left, and an identifier on its right.

You can begin a qualified name at the root/system namespace by starting out with :: such as ::Npv( cash_flow, Time, 5%). When you start at the root, it will match system objects first, and then objects that are in scope in your model namespace.

In many cases you can use an unqualified identifier to access an object that resides in a different namespace. This is possible when the identifier is unambiguously imported or is inherited by the current namespace, which are described below.

Comments


You are not allowed to post comments.