Live data from Hacker News

Anonymous functions in C

github.com

11–20 of 51 posts

Re: Anonymous functions in C

#11
post #9
post #6

Earlier quoted context omitted.

Yeah. C is a boring language that barely ever changes. But I'm not sure there isn't wisdom in being dull.

Compound literals in C are awesome like it's 1999: Assuming you have this: struct point { float x, y, z; }; void do_something_with_point(struct point *p); You can do this: do_something_with_point(&(struct point){.x = 1.5, .y = 1.5, .z = 3.5});

You can take the address of a literal?

Being able to specify a literal for a struct is useful. You can, for instance, put the literal in a macro and use the macro to initialize or reset a struct. It's better than having to write additional functions to do something trivial.

Re: Anonymous functions in C

#12
post #2

This is basically a way of obfuscating your C code. If you want to write obfuscated code, C++ has extensive support for this. I think it's actually a strength of C to not have many features. It ultimately makes code more readable and reliable.

Anonymous functions are a way of de -obfuscating code that is heavy in callbacks. Instead of having the logic flow indirectly to some external method that might be far away, the logic can live within one function body.

Callbacks are inherently going to make a program harder to follow. Without seeing the code, it's hard to suggest other solutions.

Re: Anonymous functions in C

#15
post #3

That's neat, but without closures I don't really see how you could do much with it outside of toy examples. Going off the examples -- how often do you write something like a foreach or a map or reduce that doesn't reference its enclosing scope?

You wouldn't need full blown closures. Inner procedures that can't escape their scope, like they had in Pascal are already useful but don't come with the extra memory management requirements of closures.

Re: Anonymous functions in C

#16
post #3

That's neat, but without closures I don't really see how you could do much with it outside of toy examples. Going off the examples -- how often do you write something like a foreach or a map or reduce that doesn't reference its enclosing scope?

A closure is syntactic sugar for a function plus a struct. Sometimes I think they would be a useful addition to C. But these kinds of things never turn out to be as useful as they seem at first.

Re: Anonymous functions in C

#17
post #3

That's neat, but without closures I don't really see how you could do much with it outside of toy examples. Going off the examples -- how often do you write something like a foreach or a map or reduce that doesn't reference its enclosing scope?

As long as the stack frame is still active, the local variables are still alive and valid. So you could pass a local function as a parameter, but you could not return it. I think jwz called these "downward funargs".

GCC supports it http://gcc.gnu.org/onlinedocs/gcc/Nested-Functions.html

Re: Anonymous functions in C

#18

If you use clang, blocks are another approach. [0] http://en.wikipedia.org/wiki/Blocks_(C_language_extension) [1] http://clang.llvm.org/docs/BlockLanguageSpec.html

I thought those were only available on OSX and iOS.

Nope, clang can be used on BSD, Linux, and Windows as well.

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

Re: Anonymous functions in C

#19
post #3

That's neat, but without closures I don't really see how you could do much with it outside of toy examples. Going off the examples -- how often do you write something like a foreach or a map or reduce that doesn't reference its enclosing scope?

A closure is syntactic sugar for a function plus a struct. Sometimes I think they would be a useful addition to C. But these kinds of things never turn out to be as useful as they seem at first.

That's true in a hand-wavey sense, but it ignores that function+struct means you have to define new types, include those types everywhere your closure is used, manage the lifetime and scope of those types, etc. etc.

I would argue the opposite: these things are actually way more useful than they seem at first. The fewer things the language makes you think about, the more you can focus on what you're trying to actually do.

Post reply on HN