1

Does anyone have some insight into why the block parameter of

- (void)enumerateMatchesInString:(NSString *)string
                         options:(NSMatchingOptions)options
                           range:(NSRange)range
                      usingBlock:(void (^)(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop))block

passes stop by reference instead of returning it?

It seems 'obvious' to me that you could use the return value for that which proabbly means that I am missing something and I would like to know what I am missing. (The only thing that I can think of is that you are able to provide a name for pass by reference variables to make their meaning clearer.)

griotspeak
  • 13,022
  • 13
  • 43
  • 54
  • 1
    Honestly, I don't see any particular reason for it. Maybe it's just more explicit hence it might increase readability. –  Aug 09 '13 at 23:58
  • yeah… I just edited that parenthetical in. It's all that I can think of. – griotspeak Aug 10 '13 at 00:00

1 Answers1

2

My guess is because the stop functionality is not necessarily needed, and making the block return void keeps the syntax lighter, because you can just fall off the end of the code to return:

usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
    NSLog(@"result: %@", result);
}];

rather than:

usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags) {
    NSLog(@"result: %@", result);
    return YES;
}];

Also, as you point out, there is the matter of clarity. Without checking the docs, it would be hard to tell what the return value means for enumeration here. (Also, speaking of block return values: Where's my -[NSArray collectResultsUsingBlock:] method?)

An added minor factor might be that BOOL types did not play well with block type inference for a good while, so this:

usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags) {
    return YES;
}];

would throw a type error requiring you to do either:

return (BOOL)YES;

or:

usingBlock:^BOOL (NSTextCheckingResult…

in order to make everything work out right.

All of this is just rank speculation. The documentation appears to be silent on this matter; the Coding Guidelines for Cocoa do not appear to have been updated to include even standardization efforts that Apple appears to have adopted internally, such as always providing names for the block arguments in prototype declarations.

Jeremy W. Sherman
  • 35,901
  • 5
  • 77
  • 111