| FazBrowse Wikipedia Viewer | Main Page | | |
| Viewing: https://en.wikipedia.org/wiki/Namespaces |
| Contents |
|---|
| Name conflicts |
| Examples |
| Namespace versus scope |
| In programming languages |
In computing, a namespace is a set of signs (names) 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 OS-level virtualization containers.
Similarly, hierarchical file systems organize files in directories. Each directory is a separate namespace, so that the directories "letters" and "
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:
This XML carries information about a table (i.e. a piece of furniture):
If these XML fragments were added together, there would be a name conflict. Both contain a name=<namespacename>separator<localname>
When local names are used by themselves, name resolution is used to decide which (if any) particular name is alluded to by some particular local name.
| Context | Name | Namespace name | Local name | |||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Domain name | www (subdomain name) | |||||||||||||||||||
| Wikipedia | /home/admin/Documents (directory) | C:\Users\Admin\Documents (directory) | std::chrono (C++ namespace) | Poco::Net (C++ namespace) | System.Collections.Generic (C# namespace) | java.util (Java package) | org.apache.commons.math3.special (Java package) | US (country or territory) | xhtml (previously declared XML namespace xhtml="http://www.w3.org/1999/xhtml") | $DBI (Perl module) | urn:nbn (National Bibliography Numbers) | 10 (handle naming authority) | 10.1000 (publisher) | 01-23-45 (organizationally unique identifier) | 1234 (vendor ID) | 2341 (vendor ID) | dbr (previously declared ontology, e.g. by specifying "},"parts":[{"template":{"target":{"wt":"code","href":"./Template:Code"},"params":{"1":{"wt":"@prefix dbr: <http://dbpedia.org/resource/>"}},"i":0}}]}'>@prefix dbr: <http://dbpedia.org/resource/>) | urn:isbn:978-3-16-148410-0 | ISBN | International Standard Book Numbers as Uniform Resource Names |
| International Article Number (EAN) | GS1 | 3-16-148410-X | 16 | Mohr Siebeck |
A namespace name may provide context (scope in computer science) 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 |
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:
As well as its abstract language technical usage as described above, some languages have a specific keyword used for explicit namespace control, amongst other uses. Below is an example of a namespace in C++:
namespaceabc{ intbar; }
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 intfoo;
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 using statements in headers for reasons other than backwards compatibility (e.g., convenience) is considered to be against good code practices, as those export, making foo/bar/baz/Qux.cppm).
inlinenamespace. It is akin to an implicit namespace{ classFoo; } namespacemylib{ namespace{ voidbar(); } }
To "force" a type to always be preceded by some kind of qualification, Java's "utility classes" can be mimicked; in Java and C#, a "utility class" is a class whose constructor is private, and has only System.Console.WriteLine("Hello World!"); inti=System.Convert.ToInt32("123");
Unlike C++, use* in Rust, or export.
TypeScript additionally features its own older namespace system, sometimes called "internal modules" in older versions. These use the packages (like Java), as well as packageorg.wikipedia.examples{ publicnamespaceEnglish; publicnamespaceFrench; publicclassBilingualGreeter{ Englishfunctionhello():void{ console.log("Hello, world!"); } Frenchfunctionhello():void{ console.log("Bonjour, le monde!"); } } } importorg.wikipedia.examples.BilingualGreeter; vargreeter:BilingualGreeter=newBilingualGreeter(); usenamespaceEnglish;// add all English namespace identifiers into scope greeter.hello();// invokes English version greeter.French::hello();// invokes French version
In Java, the idea of a namespace is embodied in Java packages. All code belongs to a package, although that package need not be explicitly named. Code from other packages is accessed by prefixing the package name before the appropriate identifier, for example java.lang contains classes core to the language, and import statements are not transitive nor can they be deliberately marked importjava.util.concurrent.*;). All packageorg.wikipedia.project; importjava.nio.file.Paths; importjava.util.logging.Level; importjava.util.logging.Logger; importorg.wikipdia.project.util.ConfigLoader; publicclass App{ privatestaticfinalLoggerlogger=Logger.getLogger(Main.class.getName()); publicApp(){ ConfigLoadercl=newConfigLoader(Paths.get("config/config_file.txt")); logger.log(Level.INFO,"Application starting..."); // rest of code } }
Because Java does not support independent functions outside of classes, static class methods and so-called "utility classes" (classes with private constructors and all methods and fields static) are the equivalent to C++-style namespaces. Some examples are java.lang.Math, which contains the constants like Math.PI and methods like Math.sin().
importjava.util.*; imports all classes in the java.util package. This however, can cause symbol pollution within a file. Furthermore, using glob import statements on packages that have classes with the same name can cause ambiguity, and will fail to compile. However, java.lang.* is implicitly imported into all Java source files by default.
Kotlin packages are very similar to Java packages, but unlike Java where only class-like objects may reside directly in a package, any top-level symbol (such as a variable, class, type alias, etc.) may live directly inside a package.
| View original on Wikipedia | Back | FazBrowse Home |