3 ms·
I've never had an error where I added the wrong kind of object to an array. I have, however, spent untold cumulative hours looking up the documentation of a ca
by mikeash 11y ago
I've never had an error where I added the wrong kind of object to an array.
I have, however, spent untold cumulative hours looking up the documentation of a call that took or returned NSArray* just to see what kind of objects it expected or provided in that array.
Cocoa is filled with methods like:
- (NSArray *)items;
What does it return? An array! What's in the array? Items! What class represents items? Who knows!
Sometimes it's obvious, sometimes it's not. Sometimes you have to look up the documentation and see. Sometimes, if you're really unlucky, you have to just print the thing out at runtime and see. It's almost always a pain in the ass.
Good Objective-C code will add a comment to the above to the effect of, // This returns an array of MyFooItem instances. This allows the programmer to quickly see what they can expect. Generics just formalizes this and puts it in a form that the compiler can understand. What's so bad about that?
- pavlov 11y agoThere's nothing wrong with that, it's nice. But that's all it is IMHO -- just nice. So I'm not sure why they're bothering to add it to Objective-C now. One of the reasons I've always liked Obj-C is that it is (was) such a thin layer over C, and the amount of new syntax was minimal. Devising new ways to litter code with <> brackets smells suspiciously like C++.
- mikeash 11y agoThe question of "why?" is as above, the question of "why now?" is to make it work more nicely with Swift. This addition is so distant from C++ that New Horizons hasn't even reached it yet.
- potatolicious 11y agoThey're adding it to Objective-C now to make it interop better with Swift. Objective-C classes have Swift interfaces automatically generated so you can call them from Swift code. Up until now this has been pretty shitty, since Swift is overall a stricter language than Obj-C. e.g., calls in Swift can enforce a return value to be non-nil, but Obj-C cannot, therefore all Obj-C-Swift interfaces leaves these values in an unknown nullable state even if the code itself makes guarantees on nilness. The "nice" bits being added allow proper generation of Swift interfaces so that Swift code calling Obj-C code doesn't lose type safety (or nil-safety).