Questions tagged [namespaces]

A namespace is a container that provides context for identifiers, within which names are unique.

A namespace is a container that provides context for identifiers, within which names are unique. In many implementations, identifiers can be disambiguated between namespaces by prepending the identifier with the namespace, separated by a delimiter such as a period (.) in and , double-colon (::) in or backslash (\) in .

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, but one file may have the same name multiple times.

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 (e.g., a programming language).
  • In the Java programming language, identifiers that appear in namespaces have a short (local) name and a unique long "qualified" name for use outside the namespace.
  • Some compilers (for languages such as C++) combine namespaces and names for internal use in the compiler in a process called name mangling.

PHP

Namespaces were introduced into PHP from version 5.3 onwards. In PHP, a namespace is defined with a namespace block.

namespace phpstar {
    class fooBar {
        public function foo() {
            echo 'hello world, from function foo';
        }

        public function bar() {
            echo 'hello world, from function bar';
        }
    }
}

XML

In XML, the XML namespace specification enables the names of elements and attributes in an XML document to be unique. Using XML namespaces, XML documents may contain element or attribute names from more than one XML vocabulary.

Python

In Python, namespaces are defined by the individual modules, and since modules can be contained in hierarchical packages, then name spaces are hierarchical as well. In general when a module is imported then the names defined in the module are defined via that module's name space, and are accessed in from the calling modules by using the fully qualified name.

.NET

All .NET Framework classes are organized in namespaces. When referencing a class, one should specify either its fully qualified name, which means namespace followed by the class name,

C++

In C++, a namespace is defined with a namespace block.

namespace abc {
    int bar;
}
12341 questions
7
votes
3 answers

Example of entity declared in a anonymous namespace that has external linkage

Given the statements below (emphasis mine) in §3.5/4 and in the Note [94] in §7.3.1.1/1, I'd like to have one single example of an entity declared in a unnamed namespace that has external linkage. §3.5/4 An unnamed namespace or a namespace…
Wake up Brazil
  • 3,421
  • 12
  • 19
7
votes
2 answers

Is it legal C++ to declare a nested namespace `std`?

The std namespace is special in C++, so ... Is this legal C++? // at global scope namespace mine { namespace std { ... } } I'd call it insane, but is it allowed? A reference (or non-reference) from the Standard would be appreciated.
Martin Ba
  • 37,187
  • 33
  • 183
  • 337
7
votes
1 answer

Does ADL work for the global namespace?

Examples such as enabling outputting of std types explain how ADL can be used to "inject" a certain function/operator, depending on the type the fn/op is applied to. I was wondering wheter ADL fully applies to the global namespace, that is, whether…
Martin Ba
  • 37,187
  • 33
  • 183
  • 337
7
votes
2 answers

Ruby support for XML namespaces

I work at a small company and our production system uses a hand-rolled RESTful API, implemented in Java with JAXB. We now find that we're taking on customers who use Ruby on Rails, and I have to come up with a reference implementation to show…
Alex Marshall
  • 10,162
  • 15
  • 72
  • 117
7
votes
2 answers

Is There a Benefit to Using Fully Qualified Namespaces in the Uses Clause?

Is it better to fully qualify the namespaces in the uses clause? For example, is one of these declarations better than the other? uses ShellApi, Windows, SHFolder, SysUtils; uses Winapi.ShellApi, Winapi.Windows, Winapi.SHFolder, …
7
votes
2 answers

Why were namespaces removed from ECMAScript consideration?

Namespaces were once a consideration for ECMAScript (the old ECMAScript 4) but were taken out. As Brendan Eich says in this message: One of the use-cases for namespaces in ES4 was early binding (use namespace intrinsic), both for performance…
Bob
  • 7,851
  • 5
  • 36
  • 48
7
votes
5 answers

Declaring namespace as macro - C++

In standard library, I found that namespace std is declared as a macro. #define _STD_BEGIN namespace std { #define _STD_END } Is this a best practice when using namespaces? The macro is declared in Microsoft Visual Studio…
Navaneeth K N
  • 15,295
  • 38
  • 126
  • 184
7
votes
4 answers

The type or namespace name 'Device' does not exist in the namespace 'System'

I am trying to work with GeoCoordinate which is meant to be supported by .Net Framework 4.5. I am working on winodws 8 and I have .Net Framework 4.5, but I keep getting The type or namespace name 'Device' does not exist in the namespace 'System'…
Mehraban
  • 3,164
  • 4
  • 37
  • 60
7
votes
1 answer

Include file into a namespace

I'm working with Magento, but this isn't a Magento specific question. Let's say that you're working with foo.php with contains the class Foo. In Magento, /local/foo.php will be included if it exists, otherwise /core/foo.php will be included. In…
Tyler V.
  • 2,471
  • 21
  • 44
7
votes
2 answers

JS: React and global namespace pollution

I'm considering to use react in a new website and I'm still wondering, how to handle the global namespace with react components. For example, if I define several React Components like this: var MySlider = React.createClass({ // snip }); var MyAlert…
algi
  • 567
  • 5
  • 13
7
votes
7 answers

Does using a C++ namespace increase coupling?

I understand that a C++ library should use a namespace to avoid name collisions, but since I already have to: #include the correct header (or forward declare the classes I intend to use) Use those classes by name Don't these two parameters infer…
Mike Willekes
  • 5,960
  • 10
  • 33
  • 33
7
votes
1 answer

namespaces and JDOM

My current code is printing out the xml like this : x3000 - a very fast train NULL
Sembrano
  • 1,127
  • 4
  • 18
  • 28
7
votes
1 answer

Exception: "load_missing_constant Circular dependency detected while autoloading constant" in Rails

I'm using Rails 4.0.2. I added sub directories (with model names) in Concern directory: /app/models/concerns/company/cache_concern.rb /app/models/concerns/user/cache_concern.rb /app/models/concerns/document/cache_concern.rb cache_concern.rb in…
7
votes
1 answer

Namespaces and traits

I'm getting an error using traits and namespaces, beacuse the trait can not be found. index.php: require_once 'src/App.php'; use App\main; $App = new App(); src/App.php namespace App\main; require_once __DIR__ . DIRECTORY_SEPARATOR .…
Tomás Juárez
  • 1,517
  • 3
  • 21
  • 51
7
votes
3 answers

Assembly ... internal & external namespacing

Got a quick question here. I've got an assembly being re-used by a few developers which contains various bits of functionality, but is technically split in various namespaces representing logical blocks of functionality. Now, it's being offered with…
Kevin VP
  • 71
  • 1