tags:

views:

44

answers:

3

This is mostly a follow-up to this question. I decided to just keep YAGNI in mind and created a global variable (libpython). I set it to #f initially, then set! it when init is called. I added a function that should handle checking if that value has been initialized:

  (define (get-cpyfunc name type)
    (lambda args
      (if libpython
        (apply (get-ffi-obj name libpython type) args)
        (error "Call init before using any Python C functions"))))

So now here's what I want to do. I want to define a macro that will take the following:

(define-cpyfunc Py_Initialize (_fun -> _void))

And convert it into this:

(define Py_Initialize (get-cpyfunc "Py_Initialize" (_fun -> _void)))

I've been reading through the macro documentation to try figuring this out, but I can't seem to figure out a way to make it work. Can anyone help me with this (or at least give me a general idea of what the macro would look like)? Or is there a way to do this without macros?

+1  A: 

Why don't you change the generated code to

(define Py_Initialize (get-cpyfunc 'Py_Initialize (_fun -> _void)))

and then have get-cpyfunc run (symbol->string name)?

Granted, there is probably a way to do this with syntax-case (I can never remember its syntax though), and definitely if you're using a Scheme with CL-esque define-macro.

Nathan Sanders
I suppose that's one way to do it. Hadn't thought of that.
Jason Baker
A: 

It's not the complete answer, but I came up with a macro that meets both requirements (defines a variable and a string with the name of that variable):

> (define-syntax (my-syntax stx)
  (syntax-case stx ()
  [(_ id)
  #'(define-values (id) (values (symbol->string (quote id))))]))
> (my-syntax y)
> y
"y"
Jason Baker
I can't remember `syntax-case` well enough--does `symbol-string` run at macro-expand time or at run time?
Nathan Sanders
Not in here, because `symbol->string` is part of the generated code (it's inside the template).
Eli Barzilay
+1  A: 

I've answered most of this question in the other one (I didn't see this one). It's fine to use a function that pulls out the bindings like this, but one possible problem here is that since you generate the binding only when the resulting function is called, this binding is re-created on each and every call. An easy way to solve this quickly is using promises, something like this:

(require scheme/promise)
(define (get-cpyfunc name type)
  (define the-function
    (delay (if libpython
             (get-ffi-obj name libpython type)
             (error "Call init before using any Python C functions"))))
  (lambda args (apply (force the-function) args)))

But this is essentially almost the same as the code I posted in your previous question.

More random notes:

  • get-ffi-obj will accept a symbol as the name to bind to -- this is intentional, to make such macros (as in the last question) easy.

  • Using (symbol->string 'name) in a macro is fine. As I noted above in my comment reply to Nathan's comment, this means that it gets called at runtime, but mzscheme should be able to optimize that anyway, so there's no need to try and write some sophisticated macro that does the job at compile time.

  • Look inside the PLT directory -- you will find a collection called ffi. This is a collection of examples of bindings with various styles. Macros that create the bindings are very common in these examples.

Eli Barzilay
Thank you for pointing out the examples! I was just thinking that I'd like to have more examples of using `ffi`.
Jason Baker