Why there is No constant NSArray Literal and NSDictionary Literal

122 Views Asked by At

As apple Introduced a new syntax for array literal and dictionary literals:

NSArray* literalArray = @[@"1", @"2", @"3"];

//Dictionary Literal
NSDictionary* literalDictionary = @{@1:@"first", @2:@"Second", @3:@"Third"};

They are not behaving like String literals. My question is what can be the reason that they have not introduced NSCFConstantArray class similar to NSCFConstantString? What will be the drawback to introduce a new class except backward compatibility?

2

There are 2 best solutions below

0
On BEST ANSWER

This could have been implemented, but it probably wouldn't be very useful. For this to work everything in the array or dictionary would have to be a compile-time constant as well. This would be rather limiting - without any other changes you'd only be able to create arrays and dictionaries containing nothing but strings. The number literals are not compile-time constants either. Of course those could be implemented as compile-time constants too.

But this whole thing would not be very useful. Most of the time you use those literals to put some values only known at runtime into arrays or dictionaries. It's probably not worth to go through all the trouble to implement all that.

1
On

It would entail introducing additional "inappropriate" knowledge of specific Foundation classes into the compiler, tying it more closely to Apple's frameworks rather than being a general-purpose compiler.

For example, it might interfere with using the compiler with GNUStep rather than Cocoa.

The integration with NSString is long-standing and can't be removed now, but adding more such integration would have to have a really compelling argument for it.