Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference between closures and continuations

Can someone please explain the difference between closures and continuations? The corresponding articles in wikipedia do not really compare the differences between the two.

like image 323
JRR Avatar asked Jul 28 '12 13:07

JRR


1 Answers

A closure is a function that captures data from the environment on which it was declared.

int myVar = 0;
auto foo = [&] () { myVar++; }; <- This lambda forms a closure by capturing myVar
foo();
assert(myVar == 1);

A continuation is a more abstract concept, and refers to what code should be executed afterwards. It can be implemented using a closure.

myTask = Task([] () { something(); });
myTask.then([=] () { myFoo.bar(); }); // This closure is the continuation of the task
myTask.run();
like image 139
Rodrigo Monteiro Avatar answered Nov 06 '22 09:11

Rodrigo Monteiro