I've been thinking about doing the same thing, but with actual closures/lambdas in more modern languages. Not sure if there's much of a point, though.
let result = {
let b = foo(a);
let mut c = b.see();
while (c) {
c.frob();
}
baz(c)
}; let result;
{
let b = foo(a);
let mut c = b.see();
while (c) {
c.frob();
}
result = baz(c);
}The very nice upside would be that you could make the inputs to the blocks explicit. In contrast, the fact that foo1 is an input to step1 and foo2 is an input to step2 can only be understood by careful examination.
If you define a lambda function instead of a block of code and call it right there, it would create a considerable overhead: because if it is a closure, it should save its current scope somewhere. And it is really unnecesary, if you call this function right at the same place where you define it.
AwesomenessT largerFunction(Foo1 foo1, Foo2 foo2)
{
const ResultT1 result1 = [foo1] {
const Bar bar = barFromFoo1(foo1);
const Baz baz = bar.makeBaz();
return baz.awesome();
} ();
const ResultT2 result2 = [foo2] {
const Bar bar = barFromFoo2(foo2);
return bar.awesome();
} ();
return result1.howAwesome(result2);
}
It's my understanding that compilers are already surprisingly good at optimizing out local lambdas. I recall a demo from Herb Sutter where std::for_each(someLambda) was faster than a classic for(int i;i<100000;i++) loop with a trivial body because the for_each internally unrolled the loop and the lamdba body was therefore inlined as unrolled.