From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Mark H Weaver 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 07:30:18 -0500 Message-ID: <876099cx0l.fsf@netris.org> References: <877etpwr1c.fsf@gmail.com> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Trace: blaine.gmane.org 1513254703 4952 195.159.176.226 (14 Dec 2017 12:31:43 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Thu, 14 Dec 2017 12:31:43 +0000 (UTC) User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.3 (gnu/linux) Cc: guile-user@gnu.org, Pierre LINDENBAUM To: Alex Vong Original-X-From: guile-user-bounces+guile-user=m.gmane.org@gnu.org Thu Dec 14 13:31:32 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 1ePSfj-0000aD-2G for guile-user@m.gmane.org; Thu, 14 Dec 2017 13:31:31 +0100 Original-Received: from localhost ([::1]:40482 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ePSfk-0001Gt-AG for guile-user@m.gmane.org; Thu, 14 Dec 2017 07:31:32 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:54812) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ePSfA-0001B2-1Y for guile-user@gnu.org; Thu, 14 Dec 2017 07:30:57 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ePSf5-00085u-Kp for guile-user@gnu.org; Thu, 14 Dec 2017 07:30:55 -0500 Original-Received: from world.peace.net ([50.252.239.5]:51604) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1ePSf5-0007xB-Fm for guile-user@gnu.org; Thu, 14 Dec 2017 07:30:51 -0500 Original-Received: from pool-72-93-33-26.bstnma.east.verizon.net ([72.93.33.26] helo=jojen) by world.peace.net with esmtpsa (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.89) (envelope-from ) id 1ePSet-0000Bo-UF; Thu, 14 Dec 2017 07:30:40 -0500 In-Reply-To: <877etpwr1c.fsf@gmail.com> (Alex Vong's message of "Thu, 14 Dec 2017 18:19:11 +0800") X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 50.252.239.5 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:14352 Archived-At: Hi Alex, Alex Vong writes: > 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 =E2=80=98SCM_UNDEFINED=E2=80=99 as its value. You can n= ot 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. Yes indeed, and your attached code is *almost* correct, except for one serious problem: > #include > > static SCM myfun(SCM arg1,SCM opt_arg2) > { > if (opt_arg2 =3D=3D SCM_UNDEFINED) You must not use '=3D=3D' to compare values of type SCM. Instead, use 'scm_is_eq', like this: if (scm_is_eq (opt_arg2, SCM_UNDEFINED)) Guile explicitly makes no promises about the SCM type, so there's no guarantee that '=3D=3D' will do the right thing. SCM is to be treated as an abstract type. Look up 'SCM' in the Guile manual's index. Section 6.3 (The SCM Type) of the manual states: -- C Type: SCM =E2=80=98SCM=E2=80=99 is the user level abstract C type that is used t= o represent all of Guile=E2=80=99s Scheme objects, no matter what the Scheme object type is. No C operation except assignment is guaranteed to work with variables of type =E2=80=98SCM=E2=80=99, so you should only use m= acros and functions to work with =E2=80=98SCM=E2=80=99 values. [...] Here's the Guile manual entry for 'scm_is_eq', which also mentions that you must not use '=3D=3D': -- C Function: int scm_is_eq (SCM x, SCM y) Return =E2=80=981=E2=80=99 when X and Y are equal in the sense of =E2= =80=98eq?=E2=80=99, otherwise return =E2=80=980=E2=80=99. The =E2=80=98=3D=3D=E2=80=99 operator should not be used on =E2=80=98S= CM=E2=80=99 values, an =E2=80=98SCM=E2=80=99 is a C type which cannot necessarily be compared using =E2=80=98=3D=3D=E2= =80=99 (*note The SCM Type::). Other than that, the attached code looks good to me. Mark