NSObject-Expectation icon indicating copy to clipboard operation
NSObject-Expectation copied to clipboard

default values besides nil

Open tcurdt opened this issue 12 years ago • 3 comments

This looks interesting but I think it would be nice to have default values besides nil. Maybe along the lines of:

(id)ifIsKindOfClass:(Class)aClass else:(id)value;

Not sure I like if/else naming though. Maybe this would be more expressive?

(id)nilUnlessKindOfClass:(Class)aClass;
(id)object:(id) unlessKindOfClass:(Class)aClass;

tcurdt avatar May 19 '13 08:05 tcurdt

about the naming thing: I'm not sure too, and I like your suggestions, so I think I'm going to implement them.

About the default values besides nil: yes, I want them too. But it would not work with the current syntax (because when the object I'm going to test is nil my method won't be called and return nil no matter what.)

I thought about reversing the syntax like this (maybe this is what you proposed in your last sample)

[@(42) unless:(id)objectToTest isKindOfClass:[NSNumber class]];

or maybe even

[@(42) unless:[objectToTest nilUnlessKindOfClass:[NSNumber class]]];

343max avatar May 19 '13 10:05 343max

Hm. True. And the inverted syntax makes sense. Prefer the first version though.

[@(42) unless:(id)objectToTest isKindOfClass:[NSNumber class]];

Reads much better.

tcurdt avatar May 19 '13 10:05 tcurdt

Would also be nice to elegantly handle the nil vs NSNull case.

[[NSNull null] unless:(id)objectToTest isKindOfClass:[NSNumber class]];

is probably not what we would want. Maybe have both:

unless:isKindOfClass:
nilUnlessKindOfClass:

tcurdt avatar May 19 '13 10:05 tcurdt