Is there a way to do currying in C?

后端 未结 4 1872
情话喂你
情话喂你 2020-11-27 04:34

Say I have a pointer to a function _stack_push(stack* stk, void* el). I want to be able to call curry(_stack_push, my_stack) and get back a functio

4条回答
  •  清酒与你
    2020-11-27 04:56

    I found a paper by Laurent Dami that discusses currying in C/C++/Objective-C:

    More Functional Reusability in C/C++/Objective-c with Curried Functions

    Of interest to how it is implemented in C:

    Our current implementation uses existing C constructs to add the currying mechanism. This was much easier to do than modifying the compiler, and is sufficient to prove the interest of currying. This approach has two drawbacks, however. First, curried functions cannot be type-checked, and therefore require careful use in order to avoid errors. Second, the curry function cannot know the size of its arguments, and counts them as if they were all of the size of an integer.

    The paper does not contain an implementation of curry(), but you can imagine how it is implemented using function pointers and variadic functions.

提交回复
热议问题