Namespace

Namespace

The main program acts as the root to a tree of scopes:

http://euphoria.derekparnell.id.au/namespace/tree-branches.png

A name-clash occurs when there are duplicated identifiers in a program and Euphoria is unable to determine which identifier to use.

Each branch is an independent scope; identifiers in one branch are not visible to any other branch. Inside a scope branch you can invent and use any identifier you wish. However, at the root level it is not possible to distinguish between duplicated identifiers. The result is a name-clash error:

http://euphoria.derekparnell.id.au/namespace/tree-branch1.png

The solution is to assign a namespace identifier to each scope:

http://euphoria.derekparnell.id.au/namespace/tree-branch2.png

Now each identifier is unique.

Example 0

We start with two unrelated pairs of files where:

  • bob.e includes danny.e
  • anne.e includes cathy.e

In each pair of files foo is declared as a variable, but since they are isolated files each foo is a distinct variable:

http://euphoria.derekparnell.id.au/namespace/tree0.png

On their own neither pair presents any problems.

Example 1

Now we write an application, myapp_one.ex , that includes bob.e and anne.e at the same time. This application executes without any problems:

http://euphoria.derekparnell.id.au/namespace/tree1.png

There are two distinct foo variables and two different values for foo that are printed without problems. This works because the files bob.e and danny.e and anne.e and cathy.e belong to two different scopes. Because each foo is output within an isolated scope there are no conflicts.

Example 2

Again, we write an application, myapp_two.ex , that includes both bob.e and anne.e at the same time. This time we try to output foo from the main source-code file:

http://euphoria.derekparnell.id.au/namespace/tree2.png

Now there is a serious problem. From the viewpoint of the main file there are are two different foo variables and there is no way to distinguish between them. This example crashes.

Example 3

Euphoria provides a namespace feature that lets you label the scope of a module with an identifier. The namespace identifier is then used to distinguish one foo from another foo. The final version, myapp_three.ex , adds namespace identifiers to each module as they are included:

http://euphoria.derekparnell.id.au/namespace/tree3.png

Success, it is now possible to use and output boys:foo and girls:foo as two distinct variables.

Search



Quick Links

User menu

Not signed in.

Misc Menu