kylesluder / objc-namespaces

A description (and hopefully one day an implementation) of namespaces in Objective-C
44 stars 1 forks source link

Consider wrapping namespaces in double-backticks #11

Open kylesluder opened 10 years ago

kylesluder commented 10 years ago

As of 50a7be7, I'm now using a single backtick to separate namespaces from primary identifiers:

@interface `NSObject <`NSObject>

@interface MyNamespace`MyClass (OtherNamespace`Category)

@class MyNamespace`OtherClass;

[[MyNS`Foo OtherNS`alloc] `init];

[o performSelector:@selector(`foo)];

I'm worried a single backtick isn't visible enough, especially in the @selector() form for selectors in the global namespace (presuming its replacement of the default namespace). It also means backtracking during parsing, since a namespace is a valid C identifier.

If I wrap namespaces in backticks instead, the parser immediately knows that the next identifier token is part of a namespace. Plus it's easier to read selectors:

[o performSelector:@selector(``retain)];

[o `MyNamespace` _sekretMethod:self];
akashivskyy commented 10 years ago

I would prefer colons (:). Looks more… obj-c.

kylesluder commented 10 years ago

Colons can't be used within a @selector() expression.

For example, this is currently a valid selector: @selector(::doSomething:). It names a method that takes three arguments.

akashivskyy commented 10 years ago

OK, you're right. And what about backslashes (like in PHP)? It's not like I'm bitching about backticks, but they seem very, very strange to me and almost impossible to notice…

It would look like this:

@interface \NSObject <\NSObject>

@interface MyNamespace\MyClass (OtherNamespace\Category)

@class MyNamespace\OtherClass;

[[MyNS\Foo OtherNS\alloc] \init];

[o performSelector:@selector(\foo)];

[o performSelector:@selector(\\retain)];

[o \MyNamespace\_sekretMethod:self];

This looks much more readable, IMHO.

kylesluder commented 10 years ago

The backslash is already the line continuation character as well as the escape character within strings.

The conflict with line continuation isn't an insurmountable obstacle, but it does require ensuring that the tokenizer doesn't accidentally start treating line continuations as namespaces.

The escape character is much more perilous to me. It means that NSStringFromSelector(@"\ns\aClass") doesn't work and produces no warnings. (One side effect is that one cannot use the stringification token operator to convert a namespaced identifier into a string.)