我已经定义了一个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)
答案 0 :(得分:0)
问题已在guile-user邮件列表中得到解答:http://lists.gnu.org/archive/html/guile-user/2017-12/msg00045.html
引用:Alex Vong
来自Guile手册``6.1 Guile API概述'',
对于某些Scheme函数,最后一些参数是可选的;该 必须始终使用所有可选项调用相应的C函数 指定的参数。为了获得效果,好像没有参数 指定,传递'SCM_UNDEFINED'作为其值。你不能这样做 中间的争论;当一个参数是'SCM_UNDEFINED'时 跟随它的人也必须是'SCM_UNDEFINED'。
因此,我们可以检查opt_arg2是否具有值SCM_UNDEFINED,to 决定我们是否收到了可选参数。代码在 附件:
#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);
}