github/ReactiveCocoa — pull request #142 by jspahrsummers (2012-11-27 20:39:46+00:00)
Title: Refactor -bind: to safely support side effecting binds

As currently implemented, `bind:` is critically broken, as can be seen from the test failures on 32dc98d2f210147faf8ea3ccfed26cc1584c47e1.

The failures are caused by the use of `__block` variables in methods built on `bind:` (e.g., `take:`). Because the bind is lazily evaluated any time the resulting signal is used, it's really easy to get into a state where multiple threads (or even the same thread, coroutine-style) are stomping on the `__block` variables.

Since we're trying to implement functional concepts in a fundamentally stateful language, I do think `bind:` absolutely needs to support side-effecting binds.

This refactors the method to accept a block _which returns the binding block_, so the outer block can create `__block` variables to use in the inner block. `RACSignal` and `RACSequence` make sure to evaluate the outer block as many times as needed to prevent state clobbering.