April 18, 2011

Classes
Tags blog

Blocks - A welcome addition to C

<p>In the programming environment of Mac OS X 10.6 (Snow Leopard), Apple added a programming construct to C (and by exte

In the programming environment of Mac OS X 10.6 (Snow Leopard), Apple added a programming construct to C (and by extension C++ and Objective-C) known simply as "Blocks". This new construct has been carried to the iOS platform starting with iOS 4.0. Developers familiar with the venerable LISP will smile knowingly at blocks, and wonder what took that young whippersnapper C so long. Likewise, Ruby aficionados will look at blocks and find an old friend. JavaScript developers will see in blocks echoes of anonymous functions. The concepts and behaviors that define Blocks are neither new nor revolutionary, and are implemented in a variety of languages. Seeing those concepts and behaviors implemented in a family of C languages, however, is new and exciting.

Put simply, a block is a chunk of executable code, and a lexical closure of the environment in which it was created, that can be passed around like data and executed when needed. You might think of it as a function that can be passed around like Plain Old Data, however the function carries with it the environment in which it was defined. The idea takes some getting used to, but once understood, Blocks become a powerful tool for solving problems.

Rather than post a lot of code here, I would invite you to explore this new language feature on your own. Apple has a good document which introduces a developer to the syntax and semantics of blocks here:

A Short Practical Guide to Blocks

An interesting article on the uses of blocks in different contexts can be found here:

8 ways to use Blocks in Snow Leopard

The developer community will be happy to know that Apple has submitted the specification for blocks (along with a specification for Garbage Collection) to C Standards working group for public consumption, see:

Apple's Extensions to C

That means that any C language compiler has the potential to implement blocks in the future. This has great potential to put a powerful new tool into the hands of programmers in any of a number of environments.


Feedback?

If this article contains any error, or leaves any of your questions unanswered, please help us out by opening up a github issue.
Open an issue