[CM] s7 C functions and environments

chrg chrghrc at protonmail.com
Sun Dec 29 14:40:23 PST 2024


Hello,

This might be a bit of a newbie question, but I'd like to clear this doubt. The problem I'm facing is: I want to define a function in C, and I want to pass some data to it at definition time. I thought I'd use environments for it: create a new environment to keep the data, then access it inside the function. Basically, what I'd like to do is similar to this Scheme setup:

(define (make-f)
(let ((x 1))
(lambda (y) (+ x y))))

(define f (make-f))
(display (f 2))

Except f here should be a C function.
Unfortunately, it seems C-defined functions work differently from Scheme functions. I've tried using both s7_set_curlet and s7_make_typed_function_with_environment, but no matter what I do, using s7_curlet I always get back the rootlet.

So please tell me: is this actually possible? If it's not how should I rethink this problem?

(As for why I need this: I was trying to write a small C++ wrapper for s7 and one feature I'd like to have is to be able to automatically define functions that aren't necessarily s7_function's, by doing the necessary conversions and then calling the original function. I tried doing that by declaring a template function that would take a function pointer, but then I can't figure out how to pass the function pointer to the s7_function I'm defining. You can see the work I've done here: https://github.com/chrg127/s7-tests/blob/52fdeb4692ac21e2be4e899c6c80db184f1d17c5/s7.hpp#L346)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://cm-mail.stanford.edu/pipermail/cmdist/attachments/20241229/6ee7d28b/attachment.html>


More information about the Cmdist mailing list