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
127
votes
8 answers

Correct way to define C++ namespace methods in .cpp file

Probably a duplicate, but not an easy one to search for... Given a header like: namespace ns1 { class MyClass { void method(); }; } I've see method() defined in several ways in the .cpp file: Version 1: namespace ns1 { void MyClass::method() …
Mr. Boy
  • 60,845
  • 93
  • 320
  • 589
123
votes
16 answers

Using std Namespace

There seem to be different views on using 'using' with respect to the std namespace. Some say use ' using namespace std', other say don't but rather prefix std functions that are to be used with ' std::' whilst others say use something like…
paoloricardo
  • 1,353
  • 2
  • 11
  • 10
122
votes
6 answers

Troubleshooting "The use statement with non-compound name ... has no effect"

Getting this error when I put use Blog; at the top. Warning: The use statement with non-compound name 'Blog' has no effect in... Blog is my namespace in which I have 3 classes: Article, List and Category and a few functions. If I change my…
thelolcat
  • 10,995
  • 21
  • 60
  • 102
121
votes
3 answers

This is Sparta, or is it?

The following is an interview question. I came up with a solution, but I'm not sure why it works. Question: Without modifying the Sparta class, write some code that makes MakeItReturnFalse return false. public class Sparta : Place { public bool…
budi
  • 6,351
  • 10
  • 55
  • 80
119
votes
12 answers

'namespace' but is used like a 'type'

My program uses a class called Time2. I have the reference added to TimeTest but I keep getting the error, 'Time2' is a 'namespace' but is used like a 'type'. Could someone please tell me what this error is and how to fix it? namespace TimeTest …
TheAce
  • 1,411
  • 2
  • 10
  • 9
117
votes
6 answers

C++: Namespaces -- How to use in header and source files correctly?

Consider a pair of two source files: an interface declaration file (*.h or *.hpp) and its implementation file (*.cpp). Let the *.h file be like the following: namespace MyNamespace { class MyClass { public: int foo(); }; } I have seen two…
nickolay
  • 3,643
  • 3
  • 32
  • 40
114
votes
3 answers

Why an unnamed namespace is a "superior" alternative to static?

The section $7.3.1.1/2 from the C++ Standard reads: The use of the static keyword is deprecated when declaring objects in a namespace scope; the unnamed-namespace provides a superior alternative. I don't understand why an unnamed namespace…
Nawaz
  • 353,942
  • 115
  • 666
  • 851
113
votes
11 answers

C# namespace alias - what's the point?

Where or when would one would use namespace aliasing like using someOtherName = System.Timers.Timer; It seems to me that it would just add more confusion to understanding the language.
Brad
  • 20,302
  • 36
  • 84
  • 102
112
votes
6 answers

How do I create a Python namespace (argparse.parse_args value)?

To interactively test my python script, I would like to create a Namespace object, similar to what would be returned by argparse.parse_args(). The obvious way, >>> import argparse >>> parser = argparse.ArgumentParser() >>>…
sds
  • 58,617
  • 29
  • 161
  • 278
111
votes
34 answers

"Could not load type [Namespace].Global" causing me grief

In my .Net 2.0 Asp.net WebForms app, I have my Global.asax containing the following code: <%@ Application CodeBehind="Global.asax.cs" Inherits="MyNamespace.Global" Language="C#" %> However when I build I get an error stating- Could not load type…
gkdm
  • 2,375
  • 4
  • 21
  • 27
109
votes
6 answers

Why is "using namespace X;" not allowed at class/struct level?

class C { using namespace std; // error }; namespace N { using namespace std; // ok } int main () { using namespace std; // ok } I want to know the motivation behind it.
iammilind
  • 68,093
  • 33
  • 169
  • 336
105
votes
11 answers

Remove unused namespaces across a whole project or solution at once

I know you can do it file by file. Is there any way to do this in one step for all files in a project?
leora
  • 188,729
  • 360
  • 878
  • 1,366
103
votes
9 answers

Creating a C++ namespace in header and source (cpp)

Is there any difference between wrapping both header and cpp file contents in a namespace or wrapping just the header contents and then doing using namespace in the cpp file? By difference I mean any sort performance penalty or slightly different…
links77
  • 1,534
  • 4
  • 14
  • 20
103
votes
8 answers

What does "var FOO = FOO || {}" (assign a variable or an empty object to that variable) mean in Javascript?

Looking at an online source code I came across this at the top of several source files. var FOO = FOO || {}; FOO.Bar = …; But I have no idea what || {} does. I know {} is equal to new Object() and I think the || is for something like "if it already…
Ricardo Sanchez
  • 4,935
  • 11
  • 56
  • 86
102
votes
7 answers

Using {% url ??? %} in django templates

I have looked a lot on google for answers of how to use the 'url' tag in templates only to find many responses saying 'You just insert it into your template and point it at the view you want the url for'. Well no joy for me :( I have tried every…
Robert Johnstone
  • 5,431
  • 12
  • 58
  • 88