6 ms·
I'm glad to see these additions, but the syntax for an NSArray literal bugs me. When I want an array literal in C, I'd write ... int foo[] = { 1, 2, 3, 4, 5
by openbear 15y ago
I'm glad to see these additions, but the syntax for an NSArray literal bugs me.
When I want an array literal in C, I'd write ...
int foo[] = { 1, 2, 3, 4, 5};
... but in Objective-C, for an NSArray literal I'd write ...
NSArray *bar = @[o1, o2, o3];
Why did they choose '[' over '{'? Oddly enough, NSDictionary uses '{'.
I feel like the syntax for an NSString literal is more natural (as a C developer).
NSString *baz = @"look, an NSString literal";
char szBaz[] = "look, a C string literal";
- ary 15y ago> Why did they choose '[' over '{'? Oddly enough, NSDictionary uses '{'. Who knows, but it seems to mirror Python in that respect. Being a fan of Python I can only approve.
- 2mur 15y agoCompletely agree. Looks natural to me as a python/javascript guy
- bebop 15y agoI think they chose the [] for arrays and {} for dictionaries as that is the style that seems to be most popular right now. Python for instance uses this.
- __david__ 15y agoAnd Perl. And Javascript. And Ruby. There's lots of precedent.
- openbear 15y agoWhen adding features to something, I tend to be a big fan of "when in Rome"[1]. If these were extensions to Python (or Ruby or JavaScript) then I would totally be for using '[' with arrays. As a C guy though, I would rather have seen them use '{' for NSArray and '[' for NSDictionary. Regardless, I'm glad to see this stuff added. [1] http://en.wiktionary.org/wiki/when_in_Rome,_do_as_the_Romans_do http://en.wiktionary.org/wiki/when_in_Rome,_do_as_the_Romans...
- xsmasher 15y agoIt's already custom in objc to use {} for dicts and [] for arrays when printing them out, unless I'm misremembering. It also matches the use of those characters in JSON, so it seems natural to me.
- mayoff 15y agoPerhaps they choose @[ for arrays because it requires less lookahead than figuring out whether @{ is the start of a dictionary or an array.
- icodestuff 15y agoCould be in case they want to do literal NSValues later. Since {}-syntax is used in C for both array literals and struct literals, they needed to change one, and the picked the more common one. Otherwise is foo in: id foo = @{ @2, @5 }; an NSArray containing two NSNumbers, or an NSValue containing a struct with two pointers (which happen to be NSNumber *s)? I'd guess there were other edge cases they didn't want to deal with, so they didn't do literal NSValues for this release. Or it could be that it's because []-style arrays are popular these days. Or some of both; in picking which {} to change to @[], they reasoned that lots of people have experience with []-style arrays.
- __david__ 15y agoBecause presumably (I don't have the new XCode to check) you can do things like: [someObject dictArg:@{ a:@"B" } arrayArg:@[1,2,34]]; So if they were both using "{" to set them off the parser doesn't know which one you mean until it hits the first ":" (and it can't guess from the type when the type is "id"). Now that's not impossible to write a parser that looks ahead to figure out what you mean, but it's much easier to parse if they have completely different syntax from the outset. And the "{" vs "[" distinction is very standard amongst the higher level languages, even if it isn't in straight C.