[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Newbie question on writing bindings.
From: |
Neil Jerram |
Subject: |
Re: Newbie question on writing bindings. |
Date: |
17 Apr 2001 22:22:14 +0100 |
User-agent: |
Gnus/5.0808 (Gnus v5.8.8) Emacs/20.7 |
>>>>> "Martin" == Martin Grabmueller <address@hidden> writes:
>> From: Joel Smith <address@hidden> Date: Sat, 14 Apr 2001
>> 21:59:27 +0000 (GMT)
>>
>> How do I write bindings for such functions? I thought about
>> passing the various values as a list but that does not seem
>> like the Right Thing to me. I also thought of using keyword
>> arguments like this:
>>
>> (baz #:foo #t :#bar #t)
>>
>> But I don't know how to write a C routine to handle keyword
>> arguments. I have looked at other guile code to see if anyone
>> else has solved this problem but I could not find any examples
>> of it. Surely I can't be the only person who wants to do this?
>> ;-) Any help would be much appreciated!
Martin> I haven't tried to use keywords in Guile extensions yet,
Martin> but I think you would have to
Martin> 1. Define your primitive written in C to accept
Martin> arbitrarily many arguments. [...]
Martin> But personally, I like it better to write the primitive
Martin> `baz' in a way that it can be called like
Martin> (baz 'foo 'bar) [...]
Also, as a general idea, consider defining your C primitive so that
it's easy to write in C, and then write a Scheme level wrapper
function with an interface that is convenient to call. For example:
SCM scm_baz_raw(SCM flags) /* flags is expected to be an integer */
{
int cflags;
SCM_VALIDATE_INUM_COPY (SCM_ARG1, flags, cflags);
baz_internal (cflags);
return SCM_UNSPECIFIED;
}
(define (baz . symbols)
(let ((accum 0)
(alist '((foo . 1)
(bar . 2)
(gan . 4)
...)))
(for-each (lambda (sym)
(set! accum (logior accum (assq-ref alist sym))))
symbols)
(baz-raw accum)))
In other words, it much easier to do nice interfacey stuff in Scheme
than in C.
Regards,
Neil
PS. Totally untested, of course.