[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: function registered with scm_c_define_gsubr: how can i handle an opt

From: Alex Vong
Subject: Re: function registered with scm_c_define_gsubr: how can i handle an optional parameter?
Date: Thu, 14 Dec 2017 18:19:11 +0800
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/25.3 (gnu/linux)


From the Guile manual ``6.1 Overview of the Guile API'',

>    For some Scheme functions, some last arguments are optional; the
> corresponding C function must always be invoked with all optional
> arguments specified.  To get the effect as if an argument has not been
> specified, pass ‘SCM_UNDEFINED’ as its value.  You can not do this for
> an argument in the middle; when one argument is ‘SCM_UNDEFINED’ all the
> ones following it must be ‘SCM_UNDEFINED’ as well.

Therefore, we can check if opt_arg2 has the value SCM_UNDEFINED, to
decide if we have received an optional argument. The code is in the

#include <libguile.h>

static SCM myfun(SCM arg1,SCM opt_arg2)
  if (opt_arg2 == SCM_UNDEFINED)
    scm_display(scm_from_utf8_string("Optional argument NOT received!\n"),
    scm_display(scm_from_utf8_string("Optional argument received!\n"),
  return SCM_BOOL_T;

  scm_c_define_gsubr("myfun", 1, 1, 0, myfun);

Pierre LINDENBAUM <address@hidden> writes:

> Hi all,
> (cross posted on SO: )
> I've defined a guile C function:
>     static SCM myfun(SCM arg1,SCM opt_arg2)
>       {
>       return SCM_BOOL_T;
>       }
> registered with
>     scm_c_define_gsubr ("myfun", 1, 1, 0, myfun);
> there is one optional argument. How can I detect if opt_arg2 has been
> used ?
> in
>     (myfun 1)
> or
>     (myfun 1 2)
> Thanks !

Attachment: signature.asc
Description: PGP signature

reply via email to

[Prev in Thread] Current Thread [Next in Thread]