c - GNU 欺骗 : function registered with scm_c_define_gsubr: how can i handle an optional parameter?

标签 c optional-parameters guile

我定义了一个 guile C 函数:

static SCM myfun(SCM arg1,SCM opt_arg2)
  {
  return SCM_BOOL_T;
  }

注册

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

有一个可选参数。如何检测 opt_arg2 是否已被使用?

(myfun 1)

(myfun 1 2)

最佳答案

该问题已在 guile 用户邮件列表中得到解答:http://lists.gnu.org/archive/html/guile-user/2017-12/msg00045.html

引用:Alex Vong

摘自 Guile 手册“6.1 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.

因此,我们可以检查 opt_arg2 的值是否为 SCM_UNDEFINED,以 决定我们是否收到了可选参数。代码位于 附件:

#include <libguile.h>

static SCM myfun(SCM arg1,SCM opt_arg2)
{
  if (scm_is_eq (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);
}

关于c - GNU 欺骗 : function registered with scm_c_define_gsubr: how can i handle an optional parameter?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47797521/

相关文章:

wcf - WCF 中 UriTemplate 中的可选参数

scheme - Guile Scheme 解释器中奇怪的乘法行为

python - 如何一次将所有默认参数值设置为无

方案/诡计 : Variable self-re-definition inside a function

list - 将列表放入方案的参数中

计算并删除数组 c 中的重复项

c++ - 源文件的扩展名

c - 将已经分配的内存映射到共享内存

c - 如何在套接字上正确使用 select()?

python - 如何以 python 方式拥有部分互斥的可选参数?