How Nested Functions Work, part 1

Michel Fortin michel.fortin at michelf.com
Tue Sep 1 04:32:41 PDT 2009


On 2009-08-30 17:34:36 -0400, Walter Bright <newshound1 at digitalmars.com> said:

> http://www.reddit.com/r/programming/comments/9fk6g/how_nested_functions_work_part_1/

It's 
> 
true that C doesn't have nested functions, but Apple's version of C, 
C++ and Objective-C, starting with Snow Leopard, has it. Well, it has 
nested function litterals, called blocks.

And it's really nice for performing tasks in the background. The 
following example is an Objective-C method that adds a task to the 
system's global task queue, which upon completion adds a task to the 
application's main thread event loop to updates the user interface.

- (IBAction)analyzeDocument:(NSButton *)sender
{
  dispatch_async(dispatch_get_global_queue(0, 0), ^{
    NSDictionary *stats = [myDoc analyze];
    dispatch_async(dispatch_get_main_queue(), ^{
      [myModel setDict:stats];
      [myStatsView setNeedsDisplay:YES];
      [stats release];
    });
  });
}

Source: Mac OS X 10.6 Snow Leopard Review, Ars Technica


-- 
Michel Fortin
michel.fortin at michelf.com
http://michelf.com/




More information about the Digitalmars-d mailing list