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
173
votes
5 answers

Declaring an enum within a class

In the following code snippet, the Color enum is declared within the Car class in order to limit the scope of the enum and to try not to "pollute" the global namespace. class Car { public: enum Color { RED, BLUE, WHITE …
bporter
  • 3,572
  • 4
  • 24
  • 23
171
votes
5 answers

Why can't I forward-declare a class in a namespace using double colons?

class Namespace::Class; Why do I have to do this?: namespace Namespace { class Class; } Using VC++ 8.0, the compiler issues: error C2653: 'Namespace' : is not a class or namespace name I assume that the problem here is that the compiler…
user123456
162
votes
7 answers

Should the folders in a solution match the namespace?

Should the folders in a solution match the namespace? In one of my teams projects, we have a class library that has many sub-folders in the project. Project Name and Namespace: MyCompany.Project.Section. Within this project, there are several…
80bower
161
votes
2 answers

Superiority of unnamed namespace over static?

How are unnamed namespaces superior to the static keyword?
Nawaz
  • 353,942
  • 115
  • 666
  • 851
159
votes
5 answers

How do I create a namespace package in Python?

In Python, a namespace package allows you to spread Python code among several projects. This is useful when you want to release related libraries as separate downloads. For example, with the directories Package-1 and Package-2 in…
joeforker
  • 40,459
  • 37
  • 151
  • 246
143
votes
9 answers

"using namespace" in c++ headers

In all our c++ courses, all the teachers always put using namespace std; right after the #includes in their .h files. This seems to me to be dangerous since then by including that header in another program I will get the namespace imported into my…
Baruch
  • 20,590
  • 28
  • 126
  • 201
137
votes
5 answers

how to ignore namespaces with XPath

My goal is to extract certain nodes from multiple XML files with multiple namespaces using XPath. Everything works fine as long as I know the namespace URIs. The namespace name itself remains constant, but the Schemas (XSD) are sometimes…
kostja
  • 60,521
  • 48
  • 179
  • 224
136
votes
9 answers

Namespace and class with the same name?

I'm organizing a library project and I have a central manager class named Scenegraph and a whole bunch of other classes that live in the Scenegraph namespace. What I'd really like is for the scenegraph to be MyLib.Scenegraph and the other classes to…
user430788
  • 2,143
  • 2
  • 17
  • 17
133
votes
4 answers

R: use magrittr pipe operator in self written package

I would like to use the pipe-operator %>% introduced in the magrittr package in a package I wrote myself to chain dplyr data transformations. magrittr is listed as Import in the DESCRIPTION file. After loading my own package and testing the function…
alexander keth
  • 1,415
  • 2
  • 10
  • 7
132
votes
2 answers

PHP namespaces and "use"

I am having a little trouble with namespaces and the use statements. I have three files: ShapeInterface.php, Shape.php and Circle.php. I am trying to do this using relative paths so I have put this in all of the classes: namespace Shape; In my…
Shawn Northrop
  • 5,826
  • 6
  • 42
  • 80
131
votes
1 answer

Define all functions in one .R file, call them from another .R file. How, if possible?

How do I call functions defined in abc.R file in another file, say xyz.R? A supplementary question is, how do I call functions defined in abc.R from the R prompt/command line?
G Shah
  • 2,045
  • 2
  • 16
  • 17
131
votes
3 answers

Adding System.Web.Script reference in class library

I am currently moving code from my app_code folder to a class library. I have tagged several methods with [System.Web.Script.Serialization.ScriptIgnore] attributes. My class library cannot see this namespace. My add references dialog cannot see…
user112799
  • 1,605
  • 2
  • 13
  • 15
129
votes
10 answers

Why doesn't ANSI C have namespaces?

Having namespaces seems like no-brainer for most languages. But as far as I can tell, ANSI C doesn't support it. Why not? Any plans to include it in a future standard?
Pulkit Sinha
  • 2,654
  • 4
  • 19
  • 20
129
votes
11 answers

Is there a better way to express nested namespaces in C++ within the header

I switched from C++ to Java and C# and think the usage of namespaces/packages is much better there (well structured). Then I came back to C++ and tried to use namespaces the same way but the required syntax is horrible within the header…
Beachwalker
  • 7,685
  • 6
  • 52
  • 94
128
votes
15 answers

how to change namespace of entire project?

I'm modifying demo application from this article: http://msdn.microsoft.com/en-us/magazine/dd419663.aspx I need to update all files to use my namespace, for example now file located here: MySolution\MyApp\DemoApp\ViewModel\MainWindowViewModel.cs is…
Oleg Vazhnev
  • 23,239
  • 54
  • 171
  • 305