3 ms·
The namespaces are inherent in the class name. It's actually a very nice solution. Instead of this Foundation::Array we get NSArray. Way easier to type out, an
by bennyg 13y ago
The namespaces are inherent in the class name. It's actually a very nice solution.
Instead of this Foundation::Array we get NSArray. Way easier to type out, and looks a million times cleaner to me.
- pjmlp 13y agoUntil some other cool library you really want to use happens to also have a NSArray. With proper namespaces/modules, such conflicts can be usually sorted out by renaming on import. With C and Objective-C, good luck.
- bennyg 13y agoIf a cool library you want also has a class named NSArray, then you just refactor it by renaming on import like you just mentioned. It's literally the exact same thing as what you're saying. Say I have one library in my project already - we'll call it BGAddressBook. And then I find another library that I really want to add that also happens to be named the exact same - BGAddressBook. So, I would refactor one of them (probably the one already in there to be BG1AddressBook or whatever). This is no different than having a library named BGAddressBook in ruby and wanting to use another library with the same namespace. You're going to have to change one or the other's namespace to work with both. Properties don't matter and can collide with no problem. I can have a BGAddressBook with a name property and a BGSomethingElse with a name property. Doesn't matter. It'd be the same as BGAddressBook::Name and BGSomethingElse::Name.
- pjmlp 13y agoHow do you refactor third party libraries delivered as binary?
- bennyg 13y agoHow would you refactor a third party library delivered as a binary in a language with namespaces?
- klibertp 13y agoBy importing names from that namespace and aliasing them. Or by creating another namespace and importing them there. You can't seriously argue that "NSArray" as a single string is the same thing as (hypothetical) "NS.Array", where both "NS" and "Array" and the thing at "NS.Array" are semantically different things. Don't get me wrong, I love Smalltalk and Erlang - both languages suffering from the same problem - but I can recognize a shortcoming when it bites my arm off.
- bennyg 13y agoI was genuinely asking in my question above. I'm all for it as long as it makes sense. I've never messed with aliasing or anything like that - I'm decently fluent in Objective-C but have only really dabbled in ruby/js. After looking around halfway through writing this, this looks pretty nice: http://stackoverflow.com/questions/11091740/is-it-possible-to-make-an-alias-for-a-module-in-ruby http://stackoverflow.com/questions/11091740/is-it-possible-t... I see what you're saying. Maybe it's Stockholm Syndrome, but I do like knowing associated files in 3rd party libraries easily too. I don't know, I'll experiment with this more in ruby and see how it feels.
- luikore 13y agoThen there must be some other cool library you really want to use happens to also have the namespace "std" and a "vector" class inside...
- pjmlp 13y ago#include <coollib.h> namespace betterns = std;
- swift 13y agoThe whole problem is that namespaces are open, and you can add to them from anywhere. This means a namespace alias is useless in this situation. There's no distinction between things put in namespace std by library A and things put there by library B - they are all equally valid members of namespace std. All that has been accomplished by the namespace alias is that before, both libraries were accessible through "std", and now, both libraries are accessible through both "std" and "betterns". Namespaces gives you the tools to make name clashes less likely, but they don't make them impossible. Generally to avoid clashes totally you need a central arbiter of name ownership like, say, the DNS registry, which is why naming your packages using inverted domain names was advocated by some people in the Java community.
- pjmlp 13y agoI just gave a C++ example, because you asked for one. There are lots of module systems to choose from, and Objective-C offers none.
- swift 13y agoI never asked for anything; perhaps you are confusing me with luikore. Namespaces and modules do not prevent name clashes, at least in any language that I'm aware of. Even with a more sophisticated system like Haskell's, you're still out of luck if you have a package name clash. (And getting to the point where that's the limiting factor requires GHC extensions.) What namespaces/modules buy you with respect to name clashes is just that they make it possible to stomach longer names that make those clashes less likely. Are you aware of a programming language that does truly prevent name clashes through a namespace or module system? I'd be interested to hear of it.
- mikeash 13y agoExcept that with a proper namespacing solution, you just declare that you want Foundation's Array in one spot, and in the rest of the file you just write Array. With NSArray, it's NSThis and NSThat and NSBlah and NSFoo and NSBar and NSNSNSNSNSNSNS everywhere.
- bennyg 13y agoThat's an interesting point. I've never done that (only an amateur at ruby) with a class before besides #defines, which just feels like bad juju. Another good thing about the Obj-C de facto way is that you can immediately tell what a class is vs seeing String string