This is the mail archive of the guile@cygnus.com mailing list for the guile project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Optional Arguments


James Dean Palmer wrote:
> 
> In the project I am working on, I am wanting to use optional
> arguments like:
> 
> (foo bar #:x 100 #:y 200)
> 
> where foo is a c procedure.  I thought the easiest way to handle this
> would be to make foo a procedure with one required argument, bar, and
> one rest argument, and then to parse the rest list for value pairs.
> But I guess I don't know how to do this --  What are #:x and #:y in c?
> I can't do a SCM_CHARS() to find out what they are (and yet, I can
> gh_display() them)
> 
> Any help or advice appreciated,
> 

You can do a SCM_ROCHARS on them I think, or you can store objects
representing
the keywords by using something like 

{
  SCM key_x;

  key_x = scm_dash_symbol_to_keyword("-x");
  scm_permanent_object(key_x);
}

at initialization time and then later compare to these using `gh_eq_p'
or even 
just `='. The latter approach is probably better as it will be much
faster than 
string comparison.

Another approach you could consider is to declare a C primitive taking
the ordinary
sort of optional arguments, or fixed aruments, and use `define*' or
`lambda*'
from the `(ice-9 optargs)' module to write a wrapper around it in
Scheme, as 
those provide  a convenient way to declare procedures with keyword
arguments, 
for example:

(define* (scheme-foo bar #&key (x #f) (y #f))
   (c-foo bar x y))

Keyword arguments may be supported directly in the interpreter at some
point, in which case it may be easier to use them from C, but for now I
would reccomend the Scheme-level wrapper approach.

 - Maciej