News for hackers. Stuff that matters.
Re:Anonymous functions in C by misframer on Saturday June 21, 2014 @09:01PM (#7926526)
Re:Anonymous functions in C by graphitemaster on Saturday June 21, 2014 @09:57PM (#7926636)
The problem with clang blocks is they're represented as a Objective-C object, this makes them unusable in APIs that expect a function pointer, the only way you can cast them to a function pointer is to define the structure which represents the block and mmap executable code pages to marshal the call. Such a library exists that binds them to libffi here https://github.com/mikeash/MABlockClosure This feat alone makes blocks essentially useless unless your entire API is also block-based, as in T (^foo)(...) vs T(*foo)(...).
Re:Anonymous functions in C by mtdewcmu on Saturday June 21, 2014 @11:11PM (#7926778)
I don't think they could be an ObjectiveC object, because they're described as C, not ObjectiveC.

I think the problems you're describing are ones that are going to be faced in any attempt at C closures. Closures have memory attached, that's the appeal of them and also the source of all the problems.

Re:Anonymous functions in C by graphitemaster on Saturday June 21, 2014 @11:31PM (#7926813)
Nope, they're an ObjectiveC object with some specialties at the compiler level. Google how they're implemented.
Re:Anonymous functions in C by fyolnish on Sunday June 22, 2014 @01:24AM (#7926986)
An objective-c object is just a pointer to a struct. You don't need the objective-c runtime to call a block.

Block_copy etc are implemented in libclosure which iirc does not require libobjc either. But of course if you ARE writing objc, they are valid objects and can be treated as such

Re:Anonymous functions in C by graphitemaster on Sunday June 22, 2014 @01:45AM (#7927013)
Correct, the problem is to call the function you cannot treat it as a standard function since it's pointing to a struct, the struct does contain the actual function pointer but there is an implicit `this' first argument to that function pointer which has to be that struct itself. This means you cannot use the block in an API that explicitly requires a function pointer, instead the API must specifically be aware of the block and would need to support it.
Re:Anonymous functions in C by fyolnish on Sunday June 22, 2014 @09:12AM (#7927719)
Of course, there's no way around that except to write block variants for those functions. stdlib on osx for example has block variants of most functions that take function pointers (`man qsort_b` for an example)

But if you write your program to use blocks from the start, that's not a big problem.

Re:Anonymous functions in C by mtdewcmu on Sunday June 22, 2014 @06:45AM (#7927491)
It sounds like you could say it's ObjectiveC-compatible, not ObjectiveC per se. But it's an unimportant distinction.
Re:Anonymous functions in C by mtdewcmu on Saturday June 21, 2014 @09:10PM (#7926542)
I thought those were only available on OSX and iOS.
Re:Anonymous functions in C by Moto7451 on Saturday June 21, 2014 @09:40PM (#7926595)
Nope, clang can be used on BSD, Linux, and Windows as well.

http://clang.llvm.org/get_started.html

Re:Anonymous functions in C by mtdewcmu on Saturday June 21, 2014 @09:42PM (#7926602)
I'm aware of that, but do you get blocks?
Re:Anonymous functions in C by e12e on Sunday June 22, 2014 @07:02AM (#7927530)
Eg on Debian: apt-get install libblocksruntime0 libblocksruntime-dev

http://compiler-rt.llvm.org/

Re:Anonymous functions in C by binarycrusader on Saturday June 21, 2014 @11:14PM (#7926781)
Indeed, I don't think you do:

  Blocks are supported for programs developed for Mac OS X
  10.6+ and iOS 4.0+,[1] although third-party runtimes
  allow use on Mac OS X 10.5 and iOS 2.2+.[2]
http://en.wikipedia.org/wiki/Blocks_(C_language_extension)
Re:Anonymous functions in C by nitrogen on Saturday June 21, 2014 @11:25PM (#7926804)
That page does mention Linux in one section: https://en.wikipedia.org/wiki/Blocks_%28C_language_extension...
Re:Anonymous functions in C by binarycrusader on Sunday June 22, 2014 @07:23PM (#7929556)
That's really annoying; the headline claims one thing and the later text clearly contradicts.

Regardless, both of you appear to be correct:

  BlocksRuntime - a target-independent implementation of
  Apple "Blocks" runtime interfaces.
http://compiler-rt.llvm.org/
Re:Anonymous functions in C by fyolnish on Sunday June 22, 2014 @01:27AM (#7926989)
You do, I've used them on Linux myself.