Why doesn’t Objective-C have namespaces? It seems like a simple feature that would make some class names more readable (AVMutableVideoCompositionLayerInstruction
anyone?) and axe the silly letter prefixes on class names. Is this mainly because of backwards compatibility? Is it harder to implement namespaces than it seems?

- 102,279
- 44
- 260
- 354
-
For what it's worth, I wouldn't prefer NeXTSTEP::Array over NSArray. Silly as you might find the prefix thing, I think the fact that the names don't contain any symbol characters fits nicely into the sentence-like structure of the Objective-C syntax. – w-m Nov 29 '10 at 11:24
-
@w.m: But you'sd read it as "Nextstep array" not "Nextstep colon colon array" in much the same way as you read `objectAtIndex: i` as "object at index i" not "object at index colon i". The point is moot as far as the NS- classes are concerned anyway. You can't move them to a new namespace without breaking all existing Cocoa software. – JeremyP Nov 29 '10 at 11:40
-
2Also, you wouldn't reference it as `Nextstep::Array` each time, you'd have one `using Nextstep;` and then just reference it as `Array`, *unless* there was an ambiguity and then you'd fully resolve it. It would actually make the code cleaner and more readable. – devios1 Mar 01 '13 at 22:22
-
2WHy NextStep or why not just, NS as a name space? using NS; and then just Array and MutableArray would look nice for backwards compatibility, the namespace could be converted to prefixes by the compiler. – Amogh Talpallikar Jul 11 '13 at 06:25
-
1By the way, in the hindsight the reason is probably obvious: Swift. – zoul Mar 09 '15 at 15:15
2 Answers
I don't know the answer but I suspect "it's harder than it looks" is probably it. You would have to introduce support in the compiler and linker in a way that doesn't break existing software. And while this is obviously possible (C++ has already done it) presumably the tool chain team have had higher priorities on their plate. e.g. in the recent past we have had garbage collection, GCD, blocks and Objective-C 2.0 appear so we can't say they have been doing nothing.
Namespace support is the one thing that I would dearly love to see introduced to Objective-C.

- 84,577
- 15
- 123
- 161
I don't know if you want to know if there is some official decision. But namespace like many other feature are choice, choice made by the language contributor. PHP only recently introduced Namespace, and for example Java use package that act like namespace or python use modules.
I think that there is an overhead in namespace implementation, mainly because Objective-c is dinamically typed so at runtime you have to make some check to resolve the namespace, to resolve default behaviour,etc.. and I can suppose that because Objective-c is also used in embedded enviroment (AKA iPhone) speed is very important.
You've to wrap everything I've said in a big IMHO ;D
Update:
I found this very interesting discussion http://clang-developers.42468.n3.nabble.com/Adding-namespaces-to-Objective-C-td1870848.html#a1872744 on the clang developer website explaining the reason why is definitely non-trivial to implement namespace in Obj-C.

- 4,191
- 2
- 31
- 39
-
5It would be enough, I think, to provide namespace support for class names. These would be mangled in some way so that, at run time, there would be little or no overhead. – JeremyP Nov 29 '10 at 11:36