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
2 answers

Name resolution of functions inside templates instantiated with qualified types

Consider the following C++ code example: namespace n { struct A {}; } struct B {}; void foo(int) {} template void quux() { foo(T()); } void foo(n::A) {} void foo(B) {} int main() { quux(); // Error (but works if…
Nikolaus Demmel
  • 379
  • 1
  • 3
  • 11
7
votes
1 answer

In LESS CSS, is it possible to use a namespaced variable in a call to another mixin?

In LESS CSS, is it possible to use a namespaced variable in a call to another mixin or as a default value in another mixin? Using the normal syntax, it appears not, but is there an escape sequence or other syntax I can use to achieve the effect I'm…
Nathan Piazza
  • 73
  • 1
  • 3
7
votes
3 answers

PHP namespaces and using the \ prefix in declaration

The following throws an error stating Exception can not be redeclared. namespace \NYTD\ReadingListBackend; class Exception extends \Exception { } However, removing the \ prefix in the namespace declaration does not: namespace…
Jason McCreary
  • 71,546
  • 23
  • 135
  • 174
7
votes
1 answer

Parsing XML with unknown namespaces in Oracle SQL

I'm having trouble with Oracle SQL and XMLs. I'll be getting loads of clobs of well-formed XML data from an external system to parse, interpret and fill some tables with. I wrote a solution using XMLTable, which is laid out in a view on the table…
user1766472
  • 71
  • 1
  • 2
7
votes
2 answers

Why does ADL take precedence over a function in 'std namespace' but is equal to function in user-defined namespace?

I have two snippets for ADL for demo purposes. Both snippets have been compiled by VC10, gcc & comeau C++ compilers, and the result are the same for all three. <1>ADL against using directive of a user defined namespace: #include…
RoundPi
  • 5,819
  • 7
  • 49
  • 75
7
votes
1 answer

Why do you have to include PHP file, when using namespace?

Let's say i've declared a namespace like this: Why do i still have to include that file in all other files where i want to use kitchen.php Doesn't PHP know that kitchen.php resides in Kitchen…
intelis
  • 7,829
  • 14
  • 58
  • 102
7
votes
1 answer

How to control which assembly .Net chooses in a namespace conflict?

I need to (bear with me) for some reason or another use the WinRT version of the System.Text.Encoding namespace. I can add a reference to the assembly manually and such, but it will still use mscorlib's implementation. And I can't completely remove…
Earlz
  • 62,085
  • 98
  • 303
  • 499
7
votes
2 answers

Class not found error only on production server

I'm working on a project using Silex. In a particular file, I've added a use statement to have the autoloader include a particular php file. Later in the file, I use that class. All is well on the development server, but when I move to production, I…
itsmequinn
  • 1,054
  • 1
  • 8
  • 21
7
votes
5 answers

Why does ls() in R not show global variables?

In the code below, LETTERS and letters are global, or in the global search path and reachable through another package (same thing!) > LETTERS [1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S" [20] "T" "U" "V" "W" "X"…
mda
  • 1,158
  • 14
  • 18
7
votes
3 answers

Namespace correctness

I'm trying to question my own code correctness on the following minimalist example in which a header file lifts a identifier into the current namespace. #include namespace mine { using std::string; // std::string lifted into mine …
Alexandre Bell
  • 3,141
  • 3
  • 30
  • 43
7
votes
1 answer

How do I import the entire package but exclude some in Clojure?

I want to import the entire weka.classifiers.functions package but dont want to import RBFNetwork class. (ns com.wekatest (:import (weka.classifiers Classifier Evaluation) (weka.classifiers.functions) (weka.core Attribute…
unj2
  • 52,135
  • 87
  • 247
  • 375
7
votes
2 answers

R namespace access and match.fun

I'm working on an R package where one of the functions contains a match.fun call to a function in a package that's imported to the package namespace. But on loading the package, the match.fun call can't find the function name. From Hadley Wickham's…
7
votes
2 answers

XLSX- how to get rid of the default namespace prefix x:?

I'm generating XLSX spreadsheet using OOXML SDK, and I need to get rid of x: namespace prefix. How can I achieve this? using (SpreadsheetDocument doc = SpreadsheetDocument.Open("template.xlsx", true)) { //Save the shared…
user116884
  • 81
  • 1
  • 4
7
votes
4 answers

Do non-local C++11 lambdas live in anonymous namespaces?

A recent build of GCC 4.8 gives the following code, when in a header file: auto L = [](){}; struct S { decltype(L) m; }; the following warning: test.hpp:3:8: warning: 'S' has a field 'S::m' whose type uses the anonymous namespace [enabled by…
HighCommander4
  • 50,428
  • 24
  • 122
  • 194
7
votes
4 answers

Can you put a library inside a namespace?

I am working with OpenCV and I would like to put the entire library inside it's own namespace. I've looked around quite a bit but haven't found an answer... Can you do this without modifying the library source code? If then how?
Gab Royer
  • 9,587
  • 8
  • 40
  • 58