From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Alex Vong Newsgroups: gmane.lisp.guile.user 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 Message-ID: <877etpwr1c.fsf@gmail.com> References: NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: multipart/signed; boundary="==-=-="; micalg=pgp-sha512; protocol="application/pgp-signature" X-Trace: blaine.gmane.org 1513246874 14590 195.159.176.226 (14 Dec 2017 10:21:14 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Thu, 14 Dec 2017 10:21:14 +0000 (UTC) User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.3 (gnu/linux) Cc: guile-user@gnu.org To: Pierre LINDENBAUM Original-X-From: guile-user-bounces+guile-user=m.gmane.org@gnu.org Thu Dec 14 11:21:09 2017 Return-path: Envelope-to: guile-user@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1ePQdY-0003Q2-UT for guile-user@m.gmane.org; Thu, 14 Dec 2017 11:21:09 +0100 Original-Received: from localhost ([::1]:39991 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ePQdg-00057c-53 for guile-user@m.gmane.org; Thu, 14 Dec 2017 05:21:16 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:54675) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ePQbx-0004AQ-LA for guile-user@gnu.org; Thu, 14 Dec 2017 05:19:30 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ePQbt-0000dY-2V for guile-user@gnu.org; Thu, 14 Dec 2017 05:19:29 -0500 Original-Received: from mail-pg0-x229.google.com ([2607:f8b0:400e:c05::229]:41417) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1ePQbs-0000cf-RU for guile-user@gnu.org; Thu, 14 Dec 2017 05:19:24 -0500 Original-Received: by mail-pg0-x229.google.com with SMTP id o2so3112270pgc.8 for ; Thu, 14 Dec 2017 02:19:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:references:date:in-reply-to:message-id :user-agent:mime-version; bh=dTkcdvFndwHUi8aVIYoHGaw2SbrdXDjDM3uivu+gnOk=; b=L/3IYUa0VSA13g0oBguKF+MiRQOPbt/AsEVrQa+vAMGgAYB5nuG1p3V0AbPQXAbCSn tHRgEe2t65XmZdpMOaG/clEIo3BqcUB0YogETATzL+TNgBcAytSNTvysHi99VbqAdENL YGJWNSVHqdtYmGCHyuy5x7p63M/L6hzDk0FjqDImFRlVRFJGAa5qyiXgr0xy8wMWiqQk ceLp9B4YzvMT3npbfQTeT+1XtfL59Sf2KNKNqJoaQVX1Cbl4f4GJvT7LkURYIJzCdpPm 1wGRqU3tfo8SK8VQ+cEKKj+8d0KJgRcD6S3FHdFQKuPmXSVa3wkSnrK3xuCgYkcX2XZl 2S0w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:references:date:in-reply-to :message-id:user-agent:mime-version; bh=dTkcdvFndwHUi8aVIYoHGaw2SbrdXDjDM3uivu+gnOk=; b=fDose4G7bR+VqMS3B0Iag83pj6zjfmJdVDx3YGzAg/qiepOEVCCo+D2cxIEqEXVLjM Ac4D+bTa57Drg7IkH4wf5fU81jaUDDdF9NnegKz1LZV8v7w/fVqDFq+aF6W5uoqcb0s7 syGOPU1oVb2WfugQhIoIPhAQlA5ZDhXm208ReH4djegTxENHfgeaKAZHoCIBKsBymGSb WfJyzn4PiS1zEC6c7s5KaAO0HY6xvvp6oruRByufACh98pg+iHjkpQF4l6qqub5VS8UE AtlLlp5ZFm8Byidd+UVoC8M8CXA9UdNmnJk0W1dpWG+VfgJYR/j1KsgrXxChSg7nBeap 9MTA== X-Gm-Message-State: AKGB3mKE4GCtjAmgLeQq4si5CD6VJmpzg3OxvYWMDuLwh3hFRj1k6JUp elyLCbwZ35ZGOd8dSzFMEDL0xw== X-Google-Smtp-Source: ACJfBosz0jd3yF+zYQ8OyT+/MXkbS1of+0LHzyzdepquhddAlo4iHdxV/A0lPYpj4yRVDB4sMLKwCQ== X-Received: by 10.98.63.221 with SMTP id z90mr9237155pfj.101.1513246763750; Thu, 14 Dec 2017 02:19:23 -0800 (PST) Original-Received: from debian (n218103180050.netvigator.com. [218.103.180.50]) by smtp.gmail.com with ESMTPSA id u6sm7638099pfk.126.2017.12.14.02.19.21 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 14 Dec 2017 02:19:22 -0800 (PST) In-Reply-To: (Pierre LINDENBAUM's message of "Wed, 13 Dec 2017 22:50:17 +0100") X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:400e:c05::229 X-BeenThere: guile-user@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: General Guile related discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-user-bounces+guile-user=m.gmane.org@gnu.org Original-Sender: "guile-user" Xref: news.gmane.org gmane.lisp.guile.user:14351 Archived-At: --==-=-= Content-Type: multipart/mixed; boundary="=-=-=" --=-=-= Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Hello, From=20the 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 =E2=80=98SCM_UNDEFINED=E2=80=99 as its value. You can no= t do this for > an argument in the middle; when one argument is =E2=80=98SCM_UNDEFINED=E2= =80=99 all the > ones following it must be =E2=80=98SCM_UNDEFINED=E2=80=99 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: --=-=-= Content-Type: text/x-csrc Content-Disposition: inline; filename=myfun.c #include 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); } --=-=-= Content-Type: text/plain Cheers, Alex Pierre LINDENBAUM 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 ! --=-=-=-- --==-=-= Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAEBCgAdFiEEdZDkzSn0Cycogr9IxYq4eRf1Ea4FAloyUCAACgkQxYq4eRf1 Ea6OZhAAmPzBjJQ6CHfTkK50HG2liofodkzTgQLSGaHUCttHVAeXXjFgwjooYc6g Ezz8wG/9rnEtRlFMbZGK2vDx41Ti338zYJVoVSl0yPQNbevJ3iX+7PGoYFWHtDY6 zWjRQum3rIYMQmYBubKNqhqtafxEGUiAVm0zdIsw+LT69suLbGHSyxdK86fJBaPb I3Tl6NlmjElWkHu/uFlzwChT5SIMTSj2PzCELtp+2H2mG8n4VYZZy2Nl94atRyFf IydGClJRDFZYzxJ5s8GxprC6tbt/temgtXgCjYP0l0mo6DT9yIU00k2jlSY1IExD RGGGp21+wslkz+aWjjiq85TMQg/YnjDTH4+E83hL2sOw4UryFU9AU0BHDmvITBcm Xf4MgRCnGHvPqtixxSKBeANOGEg5KLGm5WzLveaA8GZnjHutnS1n/qLQb7Aj+rxK XlzXn0zM8pxAnfMlWwLyF+yQzO2rB7ipmAC75N/5VsLVyllmCJDBHHHSI+qfybE2 CqtXV5iBHoEfJo5MkaIwbXIlNKF7lkm8HByIwSdz2I3xGEf2V3HIq11V5qp13e2X 1bkBWCIa6G9TQ7YMKS2vy/R6fDNksbF/dOwLkxTpypULFhvgLbgJQbE4/e8C0n3u 9S/Vd4i60H49qrrRDPSOn+F08EUimpflcYfEw/pmzPAC+B02hDY= =xLz4 -----END PGP SIGNATURE----- --==-=-=--