A namespace is a set that means a model, an abstract repository, or an environment created for the logical grouping of unique identifiers (that is, names ).
An identifier defined in a namespace is associated with this space . The same identifier can be independently defined in several spaces. Thus, the value associated with an identifier defined in one namespace may have (or may not have) the same value as the same identifier defined in another space. Languages with namespace support define rules that indicate to which namespace the identifier belongs (that is, its definition).
For example, Andrey works in company X, and ID (abbr. From English Identifier - identifier) as an employee equals 123. Oleg works in company Y, and his ID is also 123. The only (from the point of view of some accounting system), thanks to What Andrei and Oleg can be distinguishable with matching IDs is their affiliation to different companies. The distinction of companies in this case is a system of different namespaces (one company is one space). Having two employees in a company with the same ID presents major problems when using them, for example, by payment check, which will include an employee with ID 123, it will be very difficult to identify the employee to whom this check is intended.
Hundreds and thousands of identifiers can exist in large databases. Namespaces (or similar structures ) implement a mechanism for hiding local identifiers. Their meaning is to group logically related identifiers in the corresponding namespaces, thus making the system modular . The visibility of variables can also be restricted by specifying its memory class .
Operating systems , many modern programming languages provide support for their namespace model: use directories (or folders) as a namespace model. This allows two files with the same name to exist (as long as they are in different directories). In some programming languages (for example, C ++ , Python ), the identifiers of the names of the spaces themselves are associated with the corresponding spaces. Therefore, in these languages, namespaces can be nested into each other, forming a namespace tree . The root of such a tree is called a global namespace .
Content
Borders
In programming languages, one way of defining a namespace boundary can be the use of the so-called. scopes .
Use in languages
C ++
The namespace is determined by a block of instructions:
namespace foo {
int bar ;
}
Inside this block, identifiers can be called exactly as they were declared. But outside the block, the namespace name must be specified before the identifier. For example, outside the namespace foo the bar identifier must be specified as foo::bar . C ++ contains some other constructs that make such requirements optional. So, when adding a string
using namespace foo ;
in the code, the prefix foo:: no longer required. Another example:
namespace Namespace12
{
int foo = 0 ;
}
void func1 ()
{
using namespace Namespace12 ;
// now all names from the Namespace12 namespace will be visible here without additional prefixes
++ foo ;
}
void func2 ()
{
// and here the name needs to be clarified:
Namespace12 :: foo = 42 ;
}
Code that is not explicitly declared in the namespace is implied to be declared in the global namespace.
Namespace resolution in C ++ is hierarchical. This means that in the hypothetical namespace of еда::суп , the курица identifier will denote еда::суп::курица (if space exists). If it does not exist, then it indicates еда::курица (if this space exists). If this space does not exist, then the курица refers to the identifier in the global space.
Often, namespaces in C ++ are used to avoid name collisions.
namespace {
int a ;
void f () { /*...*/ }
int g () { /*...*/ }
}
You cannot access from one translation unit to a member of an anonymous namespace from another unit.
Although namespaces are widely used in modern code, most of the old code does not have such capabilities. For example, the entire standard C ++ language library is defined inside the namespace std , but before standardization, many components were originally defined in the global space.
You can also make visible not the whole space, but individual names inside it, for example:
namespace foo {
int bar ;
int somelse ;
}
int main () {
using foo :: bar ; // Makes visible only bar, somelse is invisible!
return 0 ;
}
Java
The idea of namespaces is embodied in Java packages . All code is defined inside the package, and this package does not need an explicitly specified name. Code from other packages is available by prefixing the package name with the corresponding identifier, for example, the String class in the java.lang can be called as java.lang.String (this method is known as the full class name ). As in C ++, Java offers a construct that makes it optional to specify the package name ( import ). However, some features (such as reflection ) require the programmer to use the full name.
Unlike C ++, namespaces in Java are not hierarchically ordered due to the syntax of the language itself. However, packages are named hierarchically. For example, all packages starting with java are part of the Java platform — the java.lang contains the base classes of the language, and java.lang.reflect contains the base classes specific to reflection (reflection).
In the Java language (as in Ada , C #, and other languages), namespaces / packages reflect semantic categories of code. For example, in C #, the namespace System contains the code implemented by the system ( .NET platform). How exactly these categories are defined and what is the depth of the hierarchy depends on the language itself.
Scopes
The function and class can be defined as an implicit namespace, which is difficult to be associated with the visibility, accessibility and period of the life of the object .
C #
There are namespaces in C #, the usage is similar to C ++.
Python
In Python, the idea of namespaces is implemented in modules. (Same as in java packages)
Javascript
Despite the lack of formal support for namespaces, they are easy to implement using the object concept of a language:
var NameSpace_1 = {};
var NameSpace_2 = new Object (); // Create two namespaces
NameSpace_1 . a = 100 ;
NameSpace_2 . a = "Strawberries" ; // Variables a - each has its own
with ( NameSpace_1 ) // Specify the default namespace
{
a + = 10 ;
NameSpace_2 . a + = a ; // The variable a of the namespace NameSpace_2 will be equal to "Strawberry 110"
}
XML
In XML, the specification of XML namespaces determines the uniqueness of the names of elements and attributes in a document, similar to the role of namespaces in a programming language. Using namespaces, XML documents can contain the names of elements or attributes from more than one XML dictionary.
<rdf: RDF
xmlns: rdf = "http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns: foaf = "http://xmlns.com/foaf/0.1/"
xmlns: rdfs = "http://www.w3.org/2000/01/rdf-schema#" >
<foaf: Person rdf: about = "#JW" >
...
xmlns (XML Namespace) is an XML namespace. Connect RDF (to create an RDF document), FOAF and RDF Schema ( RDF format ).
FOAF is also an RDF document space, so its design is checked according to the RDF dictionary (rules, specifications ).
PHP
Starting with version 5.3.0, the concept of namespace has been introduced in PHP .
<? php
namespace my \ name ; // define a new namespace
class MyClass {}
function myfunction () {}
const MYCONST = 1 ;
$ a = new MyClass ; // call inside my \ name space
$ c = new \ my \ name \ MyClass ; // use full name including namespace name
$ d = new \ globalClass ; // refer to the class from the global namespace
?>
An important point. The namespace directive should be the first line of code in the file. The exception is the declare keyword, which may precede the namespace directive. It is not even allowed to output HTML before the first construction “<? Php”.
Syntax description is on the official site of the PHP project [1] .
Common Lisp
Standard Common Lisp syntax has table namespaces that are implemented through the package system [2] . To use an identifier (symbol), you must specify its full name: the name of the package, the colon, and the name of the symbol itself [3] .
Allegro Common Lisp implements a nonstandard Common Lisp extension, a hierarchical namespace in which packages are separated by a Java -style dot, and the identifier from the packages is separated by a colon. It is also possible to refer to adjacent nodes in the hierarchy of namespaces by specifying relative paths through two points [4] . Common Lisp namespaces are dynamic — they are created, filled, and destroyed during program execution. [ source? ] , although the declarative form of their description is mainly used using the defpackage form [5] .
PureBasic
In PureBasic 5.20 , namespace support was introduced, implemented as modules. The namespace is defined by the Module and EndModule command block and does not depend on the location in the source files. This means that in one file, there can be several modules, or vice versa - the module code can be divided into several files. By default, the entire module space is hidden and in order to make its individual elements visible, they must be declared in a special block of DeclareModule / EndDeclareModule commands. Anything that is not declared in this block is not available outside the module, and an access attempt will lead to a compiler message about an access violation.
DeclareModule Count
x = 0 ; Public elements
Declare Counter ()
Enddeclaremodule
Module Count
y = 0 ; Private elements
Procedure Counter ()
y + 1
ProcedureReturn y
Endprocedure
Endmodule
Count:: x = 10 ; Writing a number to a variable (for example).
Debug Count :: Counter () ; The procedure call using the name of the module.
UseModule Count ; Display module in the current space.
Debug Counter () ; Access to public elements without specifying a module name.
UnuseModule Count ; Cancel the UseModule action.
To access module elements from another module or global space, you must specify the module name and its element, for example: Count :: x. You can also use the UseModule command, which allows you to map all visible elements of a module to the current space. Its action is canceled by the UnuseModule command. It should be noted that at the same time it is possible to display the visible elements of several modules, provided that this does not cause a name conflict. Suppose there are modules in the project with the names x, y and z.
UseModule x
UseModule y
; Code.
UseModule z
; Another code.
UnuseModule y
; Another code.
UnuseModule x
UnuseModule z
This example shows that it is possible to display several modules in the current space, as well as the fact that the sequence of the display of module elements and its cancellation is not important.
Namespace emulation
In programming languages without their own support for namespaces, spaces can be emulated by extension, using conventions for naming identifiers . For example, C libraries, such as Libpng , often use a fixed prefix for all functions and variables, which is part of their frontend. Libpng supports external identifiers such as:
- png_create_write_struct
- png_get_signature
- png_read_row
- png_set_invalid
- png_get_signature
This provides a reasonable guarantee that identifiers will be unique and thus can be used in large programs without fear of collision of identifier names .
The disadvantages of namespace emulation include :
- Lack of normal accounting for nested spaces; identifiers become too long.
- Programmers or organizations may use sharply incompatible naming conventions, thereby potentially provoking greater confusion.
- Complicated operations or query operations on groups of identifiers based on namespaces in which they are declared are processed too suboptimally or not at all feasible.
- All calls to identifiers should actually be performed with the full name of the spaces . Languages with direct support for names usually provide the programmer with the opportunity to pre-declare that they want to use some (or even all) identifiers in the program from only one space, which they can later use without specifying their belonging to the space.
Notes
- ↑ PHP: Using Namespace: Basics - Manual
- ↑ Packages (English) . www.cs.northwestern.edu. The appeal date is December 23, 2018.
- ↑ Source Code Organization . lispmethods.com. The appeal date is December 23, 2018.
- ↑ Hierarchical Packages (English) . franz.com. The date of circulation is June 10, 2017.
- ↑ CLHS: Macro DEFPACKAGE Unc . www.lispworks.com. The date of circulation is June 10, 2017.