Namespace
In computing, a namespace is a set of signs that are used to identify and refer to objects of various kinds. A namespace ensures that all of a given set of objects have unique names so that they can be easily identified.
Namespaces are commonly structured as hierarchies to allow reuse of names in different contexts. As an analogy, consider a system of naming of people where each person has a given name, as well as a family name shared with their relatives. If the first names of family members are unique only within each family, then each person can be uniquely identified by the combination of first name and family name; there is only one Jane Doe, though there may be many Janes. Within the namespace of the Doe family, just "Jane" suffices to unambiguously designate this person, while within the "global" namespace of all people, the full name must be used.
Prominent examples for namespaces include file systems, which assign names to files.
Some programming languages organize their variables and subroutines in namespaces.
Computer networks and distributed systems assign names to resources, such as computers, printers, websites, and remote files. Operating systems can partition kernel resources by isolated namespaces to support virtualization containers.
Similarly, hierarchical file systems organize files in directories. Each directory is a separate namespace, so that the directories "letters" and "invoices" may both contain a file "to_jane".
In computer programming, namespaces are typically employed for the purpose of grouping symbols and identifiers around a particular functionality and to avoid name collisions between multiple identifiers that share the same name.
In networking, the Domain Name System organizes websites into hierarchical namespaces.
Name conflicts
Element names are defined by the developer. This often results in a conflict when trying to mix XML documents from different XML applications.This XML carries HTML table information:
| Good | Bad |
This XML carries information about a table :
If these XML fragments were added together, there would be a name conflict. Both contain a element, but the elements have different content and meaning.
An XML parser will not know how to handle these differences.
Solution via prefix
Name conflicts in XML can easily be avoided using a name prefix.The following XML distinguishes between information about the HTML table and furniture by prefixing "h" and "f" at the beginning of the elements.
Naming system
A name in a namespace consists of a namespace name and a local name. The namespace name is usually applied as a prefix to the local name.In augmented Backus–Naur form:
name =
When local names are used by themselves, name resolution is used to decide which particular name is alluded to by some particular local name.
Examples
Delegation
Delegation of responsibilities between parties is important in real-world applications, such as the structure of the World Wide Web. Namespaces allow delegation of identifier assignment to multiple name issuing organisations whilst retaining global uniqueness. A central Registration authority registers the assigned namespace names allocated. Each namespace name is allocated to an organisation which is subsequently responsible for the assignment of names in their allocated namespace. This organisation may be a name issuing organisation that assign the names themselves, or another Registration authority which further delegates parts of their namespace to different organisations.Hierarchy
A naming scheme that allows subdelegation of namespaces to third parties is a hierarchical namespace.A hierarchy is recursive if the syntax for the namespace names is the same for each subdelegation. An example of a recursive hierarchy is the Domain name system.
An example of a non-recursive hierarchy are Uniform Resource Name representing an Internet Assigned Numbers Authority number.
| Registry | Registrar | Example Identifier | Namespace name | Namespace |
| Uniform Resource Name | Internet Assigned Numbers Authority | |||
| Formal URN namespace | Internet Assigned Numbers Authority | |||
| International Article Number | GS1 | Bookland | ||
| International Standard Book Number | International ISBN Agency | German-speaking countries | ||
| German publisher code | Mohr Siebeck |
Namespace versus scope
A namespace name may provide context to a name, and the terms are sometimes used interchangeably. However, the context of a name may also be provided by other factors, such as the location where it occurs or the syntax of the name.| Without a namespace | With a namespace | |
| Local scope | Vehicle registration plate | Filesystem Hierarchy Standard |
| Global scope | Universally unique identifier | Domain Name System |
In programming languages
For many programming languages, namespace is a context for their identifiers. In an operating system, an example of namespace is a directory. Each name in a directory uniquely identifies one file or subdirectory.As a rule, names in a namespace cannot have more than one meaning; that is, different meanings cannot share the same name in the same namespace. A namespace is also called a context, because the same name in different namespaces can have different meanings, each one appropriate for its namespace.
Following are other characteristics of namespaces:
- Names in the namespace can represent objects as well as concepts, be the namespace a natural or ethnic language, a constructed language, the technical terminology of a profession, a dialect, a sociolect, or an artificial language.
- In the Java programming language, identifiers that appear in namespaces have a short name and a unique long "qualified" name for use outside the namespace.
- Some compilers combine namespaces and names for internal use in the compiler in a process called name mangling.
import std;
// This is how one brings a name into the current scope. In this case, it's
// bringing them into global scope.
using std::println;
namespace box1
namespace box2
int main
Computer-science considerations
A namespace in computer science is an abstract container or environment created to hold a logical grouping of unique identifiers or symbols. An identifier defined in a namespace is associated only with that namespace. The same identifier can be independently defined in multiple namespaces. That is, an identifier defined in one namespace may or may not have the same meaning as the same identifier defined in another namespace. Languages that support namespaces specify the rules that determine to which namespace an identifier belongs.This concept can be illustrated with an analogy. Imagine that two companies, X and Y, each assign ID numbers to their employees. X should not have two employees with the same ID number, and likewise for Y; but it is not a problem for the same ID number to be used at both companies. For example, if Bill works for company X and Jane works for company Y, then it is not a problem for each of them to be employee #123. In this analogy, the ID number is the identifier, and the company serves as the namespace. It does not cause problems for the same identifier to identify a different person in each namespace.
In large computer programs or documents it is common to have hundreds or thousands of identifiers. Namespaces provide a mechanism for hiding local identifiers. They provide a means of grouping logically related identifiers into corresponding namespaces, thereby making the system more modular.
Data storage devices and many modern programming languages support namespaces. Storage devices use directories as namespaces. This allows two files with the same name to be stored on the device so long as they are stored in different directories. In some programming languages, the identifiers naming namespaces are themselves associated with an enclosing namespace. Thus, in these languages namespaces can nest, forming a namespace tree. At the root of this tree is the unnamed global namespace.
Use in common languages
C
It is possible to use anonymous structs as namespaces in C since C99.- pragma once
- include
const struct Math = ;
- include
- include "Math.h"
C++
In C++, a namespace is defined with a namespace block.namespace abc
Within this block, identifiers can be used exactly as they are declared. Outside of this block, the namespace specifier must be prefixed. For example, outside of
namespace abc, bar must be written abc::bar to be accessed. C++ includes another construct that makes this verbosity unnecessary. By adding the lineusing namespace abc;
to a piece of code, the prefix
abc:: is no longer needed.Identifiers that are not explicitly declared within a namespace are considered to be in the global namespace.
int foo;
These identifiers can be used exactly as they are declared, or, since the global namespace is unnamed, the namespace specifier
:: can be prefixed. For example, foo can also be written ::foo.Namespace resolution in C++ is hierarchical. This means that within the hypothetical namespace
food::soup, the identifier Chicken refers to food::soup::Chicken. If food::soup::Chicken doesn't exist, it then refers to food::Chicken. If neither food::soup::Chicken nor food::Chicken exist, Chicken refers to ::Chicken, an identifier in the global namespace.Namespaces in C++ are most often used to avoid naming collisions. Although namespaces are used extensively in recent C++ code, most older code does not use this facility because it did not exist in early versions of the language. For example, the entire C++ Standard Library is defined within
namespace std, but before standardization many components were originally in the global namespace. The using statement can be used to import a symbol into the current scope.The use of the
using statements in headers for reasons other than backwards compatibility is considered to be against good code practices, as those using statements propagate into all translation units that include the header. However, modules do not export using statements unless explicitly marked export, making using statements safer to use. For instance, one can import a module org.wikipedia.project.util with matching namespace org::wikipedia::project::util and then use using statements on symbols from that namespace to simplify verbose namespaces. Note that unlike other languages like Java or Rust, C++ modules, namespaces and source file structure do not necessarily match, though it is convention to match them for clarity.using should be used to simplify verbose nested namespaces when modular translation units are used.export module org.wikipedia.project.App;
import std;
import org.wikipedia.project.fs;
import org.wikipedia.project.util;
using org::wikipedia::project::fs::File;
using org::wikipedia::project::util::ConfigLoader;
using org::wikipedia::project::util::logging::Logger;
using org::wikipedia::project::util::logging::LoggerFactory;
export namespace org::wikipedia::project
C++11 introduces inline namespaces, which is such that its members are treated as if they are also members of the enclosing namespace. It is declared by writing
inline namespace. It is akin to an implicit using namespace statement, meaning qualifying symbols in it is optional.The inline property is transitive. If a namespace
a contains an inline namespace b, which in turn contains another inline namespace c, then members of c can be accessed as if they were members of a or b.A primary use case for inline namespaces is ABI compatibility and versioning. By placing different versions of an API within distinct inline namespaces, and then making the currently desired version inline, library developers can manage ABI compatibility. When a new version is released, the inline keyword can be moved to the new version's namespace, allowing users to automatically link against the new version while still enabling access to older versions through explicit qualification.
namespace mylib::utils
int main