
Apple adds closures to C family of programming languages - soundsop
http://ezrakilty.net/research/2009/06/dept_of_wheel_reinvention.html
======
tumult
This sounds cool, but I think I will just make a jumble if I try to actually
use it. What I like most about closures in most languages is how quickly it
lets you work. In C, I don't want to work quickly. I need to be methodical and
pay close attention to detail -- this usually means I'm going to keep track of
what I'm doing both in my head, in notes I'm taking, and in the actual C code.
Passing pointers along with the callbacks (for example) isn't such a big deal
when I'm already doing all of this extra work.

I mean, I'm writing C in the first place since I'm doing something low-level,
something that needs careful optimization, implementing some kind of base
system for something else, etc. Otherwise I'd just use a higher level
language.

In the context of Obj-C it makes more sense, though, since you're already
writing C while working with a higher-level language, too. You use obj-c
objects if you need refcounting or GC at the moment; I can definitely see the
usefulness of having closures in C. I know lots of people will probably use it
just fine, but for me, I think I would just make a hash of my own code.

Or maybe when I give it a shot it'll work out fine. Ok done with rant :)

------
limmeau
This seems to be relevant: [http://lists.cs.uiuc.edu/pipermail/cfe-
dev/2008-August/00267...](http://lists.cs.uiuc.edu/pipermail/cfe-
dev/2008-August/002670.html)

In short: per default, you get copies of captured variables, but you can mark
variables to be writable from blocks. In order to be able to call a block
after the function call where it was created has ended, closures may be copied
to the heap (with refcounting).

~~~
onedognight
The syntax for this is quite nice.

    
    
      call_me(^int (int x, int y) { return x + y; });
      

I like it quite a bit better than the proposed c++ lambda syntax.

    
    
      [](int x, int y) -> int { return x + y; }
    

The ^ is very ananogous to *. You could declare the above function call_me()
with something like this

    
    
      void call_me(int (^fun)(int x, int y))
      {
         fun(fun(4,5), fun(3,6));
      }
    

which is quite "C like".

------
alexgartrell
The optimist in me sees this getting used to greatly reduce the amount of code
required for many repetitive things that are often done in C. Partial
Application could be really powerful in such a verbose language, and MACRO
tricks to accomplish similar results are hard to debug and pretty ugly.

The pessimist in me sees a lot of very poor design decisions resulting from
this feature, because everyone will be excited to play with the new toy.

The realist in me does not expect to see this in ANSI C any time soon.

------
hypermatt
Read the comments on the blog, actually a lot more insightful then his post

~~~
mustpax
I think the distinction between a lambda and a closure is especially
important. Closures maintain references to the local stack and I shudder to
think the kind of bugs and unsafe memory operations that can result to writing
to bad places in the stack. Buffer overflows anyone?

~~~
bitwize
No. Closures, if implemented properly, may contain references to variables in
the enclosing lexical environment where the closure was defined.

Whether they live on the stack or in the heap is an implementation detail, and
may have everything to do with the provable dynamic extent of the closure
(e.g., downward funargs can point back onto the stack whereas upward funargs
must be heap-allocated).

------
invisible
I think this will be a neat feature for Obj-C, but it'd be a neat concept (not
in this implementation) for C/C++. Closures/lambdas aren't greatly leaving the
strict language world, but they ease development vastly. I'd like to see
closures being added/removed as if they were an array/vector/something in
C/C++.

------
asimjalis
Brilliant. Can't wait to try this out.

