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

When is a namespace::function() declaration useful?

Single File Example Here is a simple program using namespaces. #include namespace foo { void hello(); } void foo::hello() { std::cout << "hello\n"; } void foo::hello(); // Why is this syntax allowed? When is it useful? int…
Lone Learner
  • 18,088
  • 20
  • 102
  • 200
6
votes
2 answers

Namespace agnostic XPath query with element content

The namespace agnostic syntax I've seen around is confusing me. Say I have: AA BB So far I see how: /root/parent/child/text() translates…
Andy Arismendi
  • 50,577
  • 16
  • 107
  • 124
6
votes
4 answers

C++: How to force libc declarations into std::?

So, I find myself in the need of libc in my C++ program. However, I do not like the idea of sprinkling it all over the global namespace. Ideally, I'd like to force the entirety of libc into the std:: namespace so I'd have to do std::memcpy rather…
user438034
6
votes
1 answer

using namespace

what is the difference between using System; and using namespace System; is it the same thing? thanks
lital maatuk
  • 5,921
  • 20
  • 57
  • 79
6
votes
1 answer

c++ Unexplainable class “ has not been declared” error due to namespace

I have some template class that has two private static members. Users define a traits struct and provide it to the template class, which then derives from it. Then in a c++ file the user define the static members, with one member initialized from…
ByteMe95
  • 836
  • 1
  • 6
  • 18
6
votes
1 answer

Generic lambda cannot be used within a namespace?

Consider the following piece of code #include #include namespace A { template struct X { using Function = std::function; static Function f; }; template
rafalc
  • 203
  • 5
  • 9
6
votes
5 answers

Using the generic type 'System.Collections.Generic.List' requires '1' type arguments

I am trying to create a program that will create a vector, generate 100 random numbers (0 - 99) and then ask for user input whether they want the numbers sorted from High to Low or Low to High. This is the code I have so far just trying to get the…
Cistoran
  • 1,587
  • 15
  • 36
  • 54
6
votes
4 answers

Is there a quick way to remove using statements in C#?

Is there a quick way to determine whether you are using certain namespaces in your application. I want to remove all the unneccessary using statements like using System.Reflection and so on, but I need a way to determine if I am using those…
6
votes
1 answer

Disable delete namespace - Kubernetes

I created a namespace called qc for qc environment. apiVersion: v1 kind: Namespace metadata: name: {{ .Values.namespace.name | quote }} kubectl create -f namespace.yaml But I can delete this namespace anytime by running kubectl delete namespace…
Gayan
  • 1,425
  • 4
  • 21
  • 41
6
votes
2 answers

Is a using-directive in a detail namespace problematic?

Consider this library header: #include #include #include namespace Lib { namespace detail { using namespace std; template void sort_impl(istream &in,ostream &out) { vector v; { …
Davis Herring
  • 36,443
  • 4
  • 48
  • 76
6
votes
2 answers

How to avoid namespace prefix for symbols in GDB?

I'm working with a C++ library. The library uses several namespaces. When debugging, I have to prefix every symbol name with the namespace prefix. It causes a lot of extra work and typing. C++ has the using namespace X concept to make symbols…
jww
  • 97,681
  • 90
  • 411
  • 885
6
votes
1 answer

How to get the namespace of a var in Clojure?

I am currently using the following which works - but it seems a bit of a cludge (defn get-namespace [qualified-var] {:pre [(var? qualified-var)]} (the-ns (symbol (apply str (drop 2 (first (str/split (str qualified-var)…
beoliver
  • 5,579
  • 5
  • 36
  • 72
6
votes
1 answer

Access local variables of overridden parent method

How can I access the local variables of a super class method in an overridden method in the subclass? class Foo(object): def foo_method(self): x = 3 class Bar(Foo): def foo_method(self): super().foo_method() …
Alejandro
  • 623
  • 6
  • 16
6
votes
1 answer

Name binding in `except` clause deleted after the clause

How can I stop Python from deleting a name binding, when that name is used for binding the exception that is caught? When did this change in behaviour come into Python? I am writing code to run on both Python 2 and Python 3: exc = None try: 1/0 …
bignose
  • 30,281
  • 14
  • 77
  • 110
6
votes
1 answer

namespace clashes in Julia

When I get libraries with the using keyword I get warnings in the console on startup. How can I mitigate the problem of name clashes? I don't see library alias keyword as which is available in other programming languages.
m33lky
  • 7,055
  • 9
  • 41
  • 48