Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is it forbidden to open multiple namespaces at a stretch?

It's possible to do using namespace foo::bar; (i.e., using the inner namespace without using the outer namespace first / at all), why does the standard forbid to do the following?

namespace foo::bar {
  // open nested namespace bar in foo and extend it...
}

I'm not looking for a workaround, just a possible rational on why this isn't allowed.

like image 325
Warren Seine Avatar asked Jun 12 '11 15:06

Warren Seine


People also ask

Can you use multiple namespaces?

A namespace is a declarative region that provides a scope to the identifiers (names of functions, variables or other user-defined data types) inside it. Multiple namespace blocks with the same name are allowed.

What do you mean by namespace?

A namespace is a declarative region that provides a scope to the identifiers (the names of types, functions, variables, etc) inside it. Namespaces are used to organize code into logical groups and to prevent name collisions that can occur especially when your code base includes multiple libraries.


1 Answers

I'm not sure "forbidden" is the right word - maybe it was just an oversight. It's a fairly small nice-to-have which isn't really a big deal.

You could also take the point of view that the namespace foo isn't created yet when you write foo::bar, so allowing that syntax makes it look like foo was already created when it was not.

You could also go further and request the ability to write class Foo::MyClass {... to define MyClass in namespace Foo, and the same for functions, variables, etc. But is this feature really necessary and solving any particular pressing problem?

like image 136
AshleysBrain Avatar answered Sep 27 '22 17:09

AshleysBrain