What is the best way to solve an Objective-C namespace collision?
stackoverflow.com
stackoverflow.com
As the developer behind one of the ad network SDKs included in countless iOS apps, I struggled finding a good solution to this problem. Spoiler: there isn't one.
The good news is, while there were often collisions on SBJson; pretty much all versions of the version are compatible. For a while, we would compile our static library without the SBJson objects, and ship the library and SBJson separately.
Recently we decided to create our own fork of SBJson, and switch to using our own prefix, which allowed us to embed it in our library, and not worry about what other libraries were doing.
Note: another tricky gotcha with static libraries for iOS: There is a bug in the linker, in that it will not import Objective-C categories from `.o` files unless there is another another symbol that is recognized, such as a class of C function. This has caused crashers and much headache.
c.f. the "Important" box in http://developer.apple.com/library/mac/#qa/qa1490/_index.htm...
Still, this is for me the preferred approach.
Perhaps this could be avoided by just replacing all class names with macros (e.g. _N(NILinkedList)), though that might hinder code completion.
Now that I think about it, I see that I don't have any idea how 'dylib's work... I should look them up.
1: http://www.optshiftk.com/2012/04/draft-proposal-for-namespac...
2: http://lists.apple.com/archives/objc-language/2012/Apr/msg00...
I'm not sure I've thought through all issues with the above approach, but the whole @namespace thing seems gross overkill to me.
On the other hand, with a carefully chosen three letter prefix these kinds of collisions should be pretty rare in practice.
Namespace mangling and more importantly a lack of standard for it are not required to implement namespaces.
In Obj-C it really wouldn't be that difficult to make an @namespace directive that just prepended the namespace to the class.
@namespace com.apple.foo
@interface Bar : NSObject
@end
Could be semantically equivalent to @interface com_apple_foo_Bar
@endI wouldn't mind seeing explicit namespace support in Obj-C although I can't say I've run into any namespace clashes in my own code yet.
Why so? Since the late 70's every language that supports modules has already solved this problem, and many of them have native code compilers.
That is how you do cool demoscene stuff in 4096K or less, with lots of self rewriting code.
It has nothing to do with the VM and everything to do with the implementation of namespaces/calling conventions being 'undefined'
It's insane.
Prefixes are not a solution as they have no ways to avoid collisions.
So much for supporting large scale development with Objective-C.
I've never used namespaces in C++, and they aren't supported in C. And yet the only collisions I've seen between libraries are from un-prefixed identifiers. There's a whole pile of them in the Win32 headers of course. I ended up with a variable called "compress" once, in a function that was calling zlib. And Bink has a U32 or u32 typedef that conflicted with the same one in the code I was working on at the time.
Aside from that, no problems noted - of course, I cannot claim to have seen every program ever, and it is straightforward to engineer problems cases. But if a namespace collision is somehow news, you may assume that they are rarer in practice than you might imagine.
Yeah, very funny, especially since they don't have any other pragmatic reasons to use it, ie. it being a more dynamic OO C than C++ while retaining full C power, them having huge API libraries for it over 25 years of NeXT/OS X development, etc etc.
>Prefixes are not a solution as they have no ways to avoid collisions.
It's not like you use lots of third party libs (if any at all) in OS X.
>So much for supporting large scale development with Objective-C.
Yeah, I mean they have just those tiny projects in it, like iPhoto, Final Cut Pro, Pages, Numbers, the whole Cocoa and Cocoa Touch stack, etc.
But we need more support for large scale development, because there are many many teams of 100s of programmers that also want to write 20 million lines Objective-C programs.
And C is even worse, you just have small projects like the Linux kernel written in it, due to the severe issue of lack of namespaces.
/s
Yeah, that only works if you don't use any third party stuff.
> Yeah, I mean they have just those tiny projects in it, like iPhoto, Final Cut Pro, Pages, Numbers, the whole Cocoa and Cocoa Touch stack, etc.
There are all internal Apple projects, as such the company can decide which prefixes to use. If they happen to clash with some third party small company, bad luck for the external company.
> But we need more support for large scale development, because there are many many teams of 100s of programmers that also want to write 20 million lines Objective-C programs.
Well, most projects I work on professionally, are the typical enterprise projects with 100+ developers, scattered across the globe.
> And C is even worse, you just have small projects like the Linux kernel written in it, due to the severe issue of lack of namespaces.
While you're being sarcastic here, let me tell you that Linux kernel works, because everyone that wants to add code to the kernel is obliged to follow certain conventions.
You cannot have that with third party code that you don't control.
Specially if it is delivered as commercial closed source libraries, which is quite common in the enterprise world.
I use Play or a home-rolled solution based on vertx, though. so I don't have to worry so much about finding them.
Still has one failure though: the default import is both qualified and unqualified, I think it should have been qualified only.
Only C and Objective-C don't, and force everyone to come up with prefixes.
And when collisions do happen, you have to resort to preprocessor tricks to work around it.