我定义了一个 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/