On Apr 24, 2014, at 4:10 PM, Alex Zavatone <z...@mac.com> wrote:

> Could we throw a category on NSObject for that and then every class that 
> originates with NSObject gets that lovely method?

Not exactly, because unlike Smalltalk's nil, Objective-C's nil is *not* an 
object.  But you could switch it around:

@implementation NSObject (NilCheck)
- (id)fallbackIfNil:(id)obj { return obj ?: self; }
@end

I still don't see how

foo = [@"Something" fallbackIfNil:foo];

has any advantage over

foo = foo ?: @"Something";

which is less verbose, doesn't have the cost of a message send, and uses an 
operator the reader of the code should already know.

--Andy

> 
> Agree on the clunky bit, but so is using the @ compiler directive to 
> accomplish everything that couldn't be fit in in the first place.  Not as if 
> I know a better way to do it, but I agree, they do feel clunky.
> 
> On Apr 24, 2014, at 3:48 PM, Lee Ann Rucker wrote:
> 
>> In Smalltalk, where nil is an object like everything else, and we were 
>> working with calls out to C APIs that had a lot of required parameters, we 
>> added an "orIfNil:" that was very useful:
>> 
>> foo := bar orIfNil:10.
>> 
>> Nil
>> orIfNil:other
>> ^other
>> 
>> Object
>> orIfNil:other
>> ^self
>> 
>> It would be useful in ObjC if there were an elegant way to do it. Macros are 
>> just clunky.
> 
> _______________________________________________
> 
> Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)
> 
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
> 
> Help/Unsubscribe/Update your Subscription:
> https://lists.apple.com/mailman/options/cocoa-dev/aglee%40mac.com
> 
> This email sent to ag...@mac.com

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to