Re: Namaspaces, caling functions and classes defined in the global name space



Toby A Inkster wrote:
taps128 wrote:

I can't stop thinkign this is to complicated to read. Wouldn't be simple
to just use the GLOBAL keyword for the global namespace, so this:
A::foo() will run the function foo from the A namespace, and this
GLOBAL::A::foo() will run the static mehod foo of class A from the
global namespace.

Firstly, what did I tell you in that "Assigning variables in ifs" thread?!
"GLOBAL::A::foo()" is a waste of 6 bytes! Six precious bytes!

::A::foo() is clear when you consider namespaces to be a bit like Unix paths. "A/foo.txt" means "from the current directory, find subdirectory A, and in there, look at file foo.txt". However, "/A/foo.txt" has an entirely different meaning -- "from the root directory, find subdirectory A and in there, look at file foo.txt".

Consider namespaces, classes and methods to be like a Unix file system, but with :: as a path separator instead of /, and it all becomes clear. The "namespace" keyword then becomes equivalent to "chdir". It takes the mystery out of namespaces by making them work like something you're already familiar with.

That said, it's probably going to be pretty rare when you're going to explicitly want to reference the global namespace.

If you're coding without namespaces, everything's already in the global namespace, so you never need to explicitly reference it.

If you're coding in a namespace and you've defined your own function preg_match(), then chances are, when you call preg_match(), it's your own function that you want to run -- the main exception being that if your preg_match() function is a wrapper around the global preg_match, then your preg_match will need to call the global preg_match, and then you'll need to explicitly reference the global namespace.

Overall, I think they've managed fairly miraculously to introduce namespaces without breaking old code.


Yes but , as I've previously stated, readibilty is king. When you look at
::A::foo();

your don't know what you are looking at exactly. It could be a function foo or a method of static class from the global namespace. When you debug or mantain someone else's code that could be a very big problem, since I generally need to know what something is.
To put a pun on you byte saving ranting it would actually cost me (or someone else ) more bytes doing things this way. Why? That's becuse that line of code requires a comment. So
::A::foo();
becomes:

//ns func
::A::foo();

or
//glob stat method
::A::foo();

I don't know but it seems to me that this way of using namespaces is something that could possibly cause a lot of headaches.
I know that something else could, well, possibly break backwards compatibily, but what the cost. Messy code, nerve breakdowns, early retirements. It could well mean the end of whole economies.
I think a more rigid apporoach should be considered.
.



Relevant Pages

  • Namaspaces, caling functions and classes defined in the global name space
    ... I've been reading the namespace specification for the 5.3 relaese, and I can't stop thinking that they have complicated the thing unecessary. ... it will first try to run the function foo() from the namespace A, and if it can't find it it will run the static method of the class A from th e global namespace. ... A::foowill run the function foo from the A namespace, and this GLOBAL::A::foowill run the static mehod foo of class A from the global namespace. ...
    (comp.lang.php)
  • Re: include headers: <X>, <X.h> and global namespaces
    ... > keep standard library definitions out of the global namespace. ... C++ standard says nothing at all about what it will or will not do. ... Each name declared as an object with external linkage in a header is ...
    (comp.lang.cpp)
  • Re: Bug in execfile?
    ... If you execfilea script names not known in the context of a function are ... looked up in the global namespace, but assignments set names in the local ... x and f are put into the local namespace, but x is not local to the function ... Now if you call execfilein the global namespace of the client script the ...
    (comp.lang.python)
  • Re: Lambda forms and scoping
    ... A function carries its own local namespace, its own closure, and its ... a global statement) the global namespace. ...
    (comp.lang.python)
  • Re: Frames as Rodney Dangerfield
    ... If you use frames you stop having "the global namespace" ... The term 'namespace' has no technical meaning in javascript (it does ...
    (comp.lang.javascript)