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
6
votes
1 answer

Redeclaration in a namespace that does not enclose the original declaration

Namespace member can be defined in a namespace that encloses the declaration’s namespace: Members of a named namespace can also be defined outside that namespace by explicit qualification (3.4.3.2) of the name being defined, provided that the…
igntec
  • 1,006
  • 10
  • 24
6
votes
1 answer

Is it possible to narrow ruby constant lookup

I have a module, that contains class named String (amongst others.) I need to lookup the class by name and gracefully fall back if there is no such a class. module Mod1 module String end end Mod1.const_get 'String' #⇒…
Aleksei Matiushkin
  • 119,336
  • 10
  • 100
  • 160
6
votes
4 answers

Clojure: fully qualified name of a function

In Clojure, is there a more elegant way of finding the fully qualified name of a function (known to have meta info) than (defn fully-qualified-name [fn] (let [fn-meta (meta fn ) fn-ns (ns-name (:ns fn-meta)) ] (str fn-ns "/"…
chris
  • 2,473
  • 1
  • 29
  • 28
6
votes
4 answers

Namespace & Class Conflict

I'm facing a problem with a conflict between the DateTime class and a namespace for some unknown reason was also called DateTime. Assembly CompanyDateTime has namespace Company.DateTime My Application is in the namespace: Company the problem is that…
Viv
  • 2,515
  • 2
  • 22
  • 26
6
votes
3 answers

Unnecessary use of unnamed namespaces C++

I keep seeing code like this everywhere in my company: namespace { const MAX_LIMIT = 50; const std::string TOKEN = "Token"; } I am confused as of why you need an anonymous namespace here. On one hand, you want a local translation unit for…
FCR
  • 1,103
  • 10
  • 25
6
votes
1 answer

SWIG - Problem with namespaces

I'm having trouble getting the following simple example to work with SWIG 1.3.40 (and I also tried 1.3.31). The Foo structure comes through as a Python module as long as I don't wrap it in a namespace, but as soon as I do I get a compilation error…
Magnus W
  • 180
  • 1
  • 6
6
votes
1 answer

Use of Namespaces in Groovy MarkupBuilder

I want have the following output: ZH
haschibaschi
  • 2,734
  • 23
  • 31
6
votes
1 answer

Java name collision between variable and top-level package name

Triggered by this bug report AVRO-1814 I reduced the problem to this minimal example case in Java that simply shows the core of the effect. package nl.basjes.experiment; public class NamingClash { String nl = "foo"; public void test() { …
Niels Basjes
  • 10,424
  • 9
  • 50
  • 66
6
votes
1 answer

Referencing and using JScript.NET "functions only" exe assembly

1. Compiled Assembly from JSC I've compiled what is intended to be client-side JavaScript using the JScript compiler (jsc.exe) on the server side in an attempt to make something that can be tested from a unit testing project, and maybe even…
John K
  • 28,441
  • 31
  • 139
  • 229
6
votes
1 answer

Composer autoload custom classes

I try to autoload my custom pdo class with composer. Ran the following command to update autoload: compser update composer install Both seem to work, no error prompted. But, vendor/composer/autoload_namespaces.php Does not list the custom…
user4164128
6
votes
1 answer

Forward declaring a function in a namespace inside another function in that namespace

I have two source files, a.cpp and b.cpp. In a.cpp, I have a function, foo: namespace ns { void foo() { std::cout << "foo!"; } } In b.cpp, I have another function in namespace ns in which I'd like to prototype and call foo: namespace ns { void…
user4520
  • 3,401
  • 1
  • 27
  • 50
6
votes
2 answers

Get available clojure namespaces

Is there an idiomatic way to get available namespaces that can be used? (all-ns) returns only already used namespaces. (Package/getPackages) returns all Java packages available for import, but only those Clojure namespaces that are already…
Adam Schmideg
  • 10,590
  • 10
  • 53
  • 83
6
votes
2 answers

Can a namespace start with a number in PHP?

When declaring the following namespace:
hielsnoppe
  • 2,819
  • 3
  • 31
  • 56
6
votes
1 answer

Passing a window object into Javascript namespace

I'm trying to better understand namespacing in javascript and found an example of a javascript Immediately-invoked Function Expression that is taking the window object as a parameter. Here is the code from it: var CG = CG || {}; CG.main =…
loremIpsum1771
  • 2,497
  • 5
  • 40
  • 87
6
votes
1 answer

Does use statement order affect functionality in PHP?

I've been using PHP's namespaces for some time now and I think it is a great addition to my programming. This morning I wondered about something concerning the use statement. I'm wondering if the order of use affects the functonality of my PHP…
Peter
  • 8,776
  • 6
  • 62
  • 95