[cfe-dev] Adding namespaces to Objective-C

David Chisnall theraven at sucs.org
Tue Nov 9 14:15:10 PST 2010


I proposed a mechanism for adding namespaces to Objective-C a short while ago, so I'll add some comments here:

On 9 Nov 2010, at 21:54, Owen Shepherd wrote:

> 
> On 9 Nov 2010, at 20:25, Chris Lattner wrote:
> 
>> On Nov 9, 2010, at 9:47 AM, Owen Shepherd wrote:
>>> As someone interested in contributing to Clang - and, in particular, contributing to its Objective-C(++) support, I would like to hear people's thoughts with regards to adding support for namespaces to Objective-C.
>> 
>> Hi Owen,
>> 
>> We've considered adding namespaces to ObjC many times in the past.  It hasn't happened so far largely due to the fact that we really want a unified way to get selectors and classes into namespaces.  Just getting classes into namespaces doesn't solve the whole name conflict issue.

The largest problem with conflicting selectors is when you define new ones with different types.  This is already solved in the GNUstep problem.  The secondary problem, of overlapping categories, would be better solved by adding class boxes to the language than trying to hack them into namespaces.  Indeed, class boxes would be a more interesting addition than namespace in general.

>>> Some thoughts:
>>> 1.	 Do we make this an extension to Objective-C, or to C itself? From my point of view, each way primarily boils 
>>> 	down to a matter of syntax: As an Objective-C extension, the keyword would likely be "@namespace"; while 
>>> 	as a C extension it would probably be "__namespace__" (or maybe just "namespace" in a hypothetical 
>>> 	"clang99"  mode)
>> 
>> This is another big issue.  There aren't a lot of good answers here.  If you decide that it should be added to C, then it is logical that C and C++ namespaces work the same way.  If you do this, then you're changing the behavior of existing ObjC++ code that does:
>> 
>> namespace abc {
>> @interface xyz
>> 
> 
> Perhaps the best option is to make these extensions optional, and when not enabled to generate a warning about incompatibility of such declarations

Adding namespaces to C requires some kind of name mangling, so it introduces all sorts of compatibility problems (much like the problems that plagued C++ compilers for years).  

>> If you make it *objc only* (e.g. "@namespace") then it really can only apply to classes, which makes the extension fit even more poorly into the language (why classes but not globals, functions, or selectors?)

I don't see applying it to anything other than classes makes sense.  Objective-C is a hybrid language.  Once you are in 'object land' (as Tom Love described the stuff inside square brackets), you don't expect things to behave in the same way as they do outside.

> I was referring more as to whether it would be supported in C, or in Objective-C only. Supporting C also would seem more satisfying.
> 
>> Depending on how crazy you get, you could imagine coming up with some sort of way to retroactively make NS::Object resolve to NSObject or something.
> 
> The obvious solution would seem to be to move the classes into the NS namespace, then make the compatibility layer do "@interface NSObject: NS::Object" for each of the classes.

That would be a terrible idea.  As is using : in separators, as it is already used in selectors (including double-colons).  One of the key design goals of Objective-C was that new semantics should ALWAYS have new syntax (Apple completely disregarded this with property accessors, which is why there was so much hostility to these).

I proposed that namespaces should be able to declare a short form, as well as the long form, e.g.:

@namespace Foundation (NS)
@interface Object ...

Would declare the Object class in the global namespace as NSObject, and in the Foundation namespace as Foundation!Object.  The runtime functions for iterating over classes would only see the version in the global namespace and a set of new functions would provide introspection.

>>> 2.	Function name mangling. Function names follow the platform's C++ ABI (Much like the existing 	
>>> 	__attribute__((overloadable)) does*
>>> 3.	When class names are involved in Objective-C symbols, then namespaced classes are mangled similarly to 
>>> 	C++ names are; a hypothetical MyNS::MyClass is mangled as 4MyNS7MyClass. This is backwards 	
>>> 	compatible; traditional class names cannot start with numbers
>>> 4.	The added namespace declarations are equivalent to and interchangeable with C++ " namespace" 
>>> 	declarations. 
>> 
>> Yes, making it as similar as possible to c++ namespaces is the only way to go.  However, you probably don't want argument dependent lookup (aka Koenig lookup), right?  What other things would be different?
> 
> Lookup would always select the entity of the same name in the closest scope, correct. The intention is that code which is valid (Obj)C would always be valid (Obj)C++ (though obviously not the converse). 

Lots of ObjC is not valid ObjC++ already; unlike ObjC, C++ is not a pure superset of C.

Mangling ObjC class names is very messy, because Objective-C classes are exposed to introspection.  They are not exposed as global symbols (necessarily - they are in some implementations, but they are already mangled there and the mangling is private and not part of the language).

Making it interchangeable with C++ namespaces also seems like a mistake.  What problem does this solve?  Objective-C classes are not interchangeable with C++ classes, neither are ObjC exceptions and so on, so why should namespaces?

>> Adding namespaces to ObjC is a huge minefield, not something to be done lightly.  If you'd really really like to pursue it, please start by writing out a spec/proposal of how you think it should work along with the corner cases enumerated.
> 
> I am drafting one now, though it may take a while.
> 
> The hardest aspect seems to be namespacing of selectors; it seems to be an issue which I haven't really seen affecting other languages, and seems a rather difficult one to solve elegantly, though I must ask: Is it a major issue in practice? I haven't found it to be, and haven't found other people claiming it to be either, though I don't claim that it doesn't exist. 

See the classboxes concept from the University of Berne.  I'd love to see ObjC get support for classboxes, but it would require massive changes to the runtime and I have yet to come up with a way of doing it that wouldn't harm performance considerably.

David



More information about the cfe-dev mailing list