guile-user
[Top][All Lists]
Advanced

[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)

Hello,

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
attachment:

#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_current_output_port());
  else
    scm_display(scm_from_utf8_string("Optional argument received!\n"),
                scm_current_output_port());
  return SCM_BOOL_T;
}

void
init_myfun(void)
{
  scm_c_define_gsubr("myfun", 1, 1, 0, myfun);
}
Cheers,
Alex

Pierre LINDENBAUM <address@hidden> writes:

> Hi all,
>
> (cross posted on SO: https://stackoverflow.com/questions/47797521 )
>
>
> 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]