default values besides nil
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;
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]]];
Hm. True. And the inverted syntax makes sense. Prefer the first version though.
[@(42) unless:(id)objectToTest isKindOfClass:[NSNumber class]];
Reads much better.
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: