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

Is it possible to add a C++ namespace to all symbols from a C library?

I'm modifying a large C++ project, which defines in one of its main headers an enum FooBar. That enum gets included everywhere, and sadly is not namespaced. From that project I'd like to use a C library, which unfortunately also defines an enum…
Johan Bilien
  • 412
  • 2
  • 12
6
votes
5 answers

Namespace Rule of Thumb

Is there a general rule of thumb as to how many classes, interfaces etc should go in to a given name space before the items should be further classfied in to a new name space? Like a best practice or a community preference? Or is this all personal…
Frank V
  • 25,141
  • 34
  • 106
  • 144
6
votes
2 answers

Multiple namespace package info Java

I have this package info /** * Created by mflamant on 13/02/2017. */ @javax.xml.bind.annotation.XmlSchema(namespace = "namespace1", xmlns = {@XmlNs(prefix = "ns4", namespaceURI = "namespace1")}, elementFormDefault = XmlNsForm.QUALIFIED) package…
Kraven
  • 245
  • 1
  • 3
  • 16
6
votes
2 answers

SQL Server Xml Namespace Querying Problem

I have the following in an xml variable @ResultData JournalNum
CaffGeek
  • 21,856
  • 17
  • 100
  • 184
6
votes
2 answers

How to define some macros as 'private' to a module, when using `macro_use` in Rust?

I have a file which contains shared macros for a Rust project. Since I want to re-use the macros across the crate (between modules), I declare the module with #[macro_use]. While writing the macros, I split out some shared logic into a macro to…
ideasman42
  • 42,413
  • 44
  • 197
  • 320
6
votes
1 answer

Docker user namespacing map user in container to host

I'd like to map user postgres or root inside container to user myuser on the host. There's a lot of references to this online, but it is not very clear to me how to achieve the following: Is there a simple way to map arbitrary user inside container…
alpha_cod
  • 1,933
  • 5
  • 25
  • 43
6
votes
1 answer

Is it bad for performance/loading to add a dynamic namespacer for your css classes via a component property?

Imagine this: export class MyComponent { namespace: string; constructor(private globals: Globals) { this.namespace = globals.namespace; } } And then the template like this:
Chrillewoodz
  • 27,055
  • 21
  • 92
  • 175
6
votes
4 answers

What does the using directive do, exactly?

On MSDN I can read what it does, but I would like to know what it does technically (tells compiler where to look for types..)? I mean using as a directive.
Loj
  • 1,159
  • 3
  • 14
  • 23
6
votes
3 answers

Documenting re-exported functions in an R package

I'm in the process of splitting one of my R packages into two, since it incorporates two logically distinct sets of functionality, one of which is more general than the other. However, since the original package is reasonably popular, and is…
Jon Clayden
  • 420
  • 2
  • 13
6
votes
2 answers

avoiding redefinition of variables for single header

I've a single header requirement on a code, which means there should be no splitting of declarations and definitions into separate header and source files. I've implemented it properly and it works as intended for my use case, where this header file…
Abhinav Gauniyal
  • 7,034
  • 7
  • 50
  • 93
6
votes
1 answer

Using statements before or after Namespace in C#

Possible Duplicate: Should Usings be inside or outside the namespace So there are two approaches to where you have your using statements in regard to the namespace. You can either have them outside the namespace declaration or inside. What is the…
Kenoyer130
  • 6,874
  • 9
  • 51
  • 73
6
votes
2 answers

How to override exported function from R package listed in Imports

My package's DESCRIPTION file has httr in its Imports directive: Imports: httr (>= 1.1.0), jsonlite, rstudioapi httr exports an S3method for length.path. S3method(length,path) And it's defined as: #' @export length.path <- function(x)…
Nicole White
  • 7,720
  • 29
  • 31
6
votes
1 answer

How do I parse and write XML using Python's ElementTree without moving namespaces around?

Our project gets from upstream XML of this form:
Viktor Haag
  • 3,363
  • 1
  • 18
  • 21
6
votes
0 answers

Swift operator defined in multiple libraries; "ambiguous"

I am have to external libraries I am using in an iOS project pod 'SQLite.swift', '~> 0.10.1' pod 'ObjectMapper', '~> 1.3' In a class, I am trying to use both: import Foundation import SQLite import ObjectMapper class SqlLiteDataManager { …
Scott Ross
  • 123
  • 1
  • 7
6
votes
4 answers

Modify namespace of importing script in Python

I'd like a function in my module to be able to access and change the local namespace of the script that's importing it. This would enable functions like this: >>> import foo >>> foo.set_a_to_three() >>> a 3 >>> Is this possible in Python?
Luke Taylor
  • 8,631
  • 8
  • 54
  • 92