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 21:50:59 +0800 Message-ID: <87374dwh8c.fsf@gmail.com> References: <877etpwr1c.fsf@gmail.com> <876099cx0l.fsf@netris.org> 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 1513259676 17468 195.159.176.226 (14 Dec 2017 13:54:36 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Thu, 14 Dec 2017 13:54:36 +0000 (UTC) User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.3 (gnu/linux) Cc: guile-user@gnu.org, Pierre LINDENBAUM To: Mark H Weaver Original-X-From: guile-user-bounces+guile-user=m.gmane.org@gnu.org Thu Dec 14 14:54:30 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 1ePTy1-0004Cz-FO for guile-user@m.gmane.org; Thu, 14 Dec 2017 14:54:29 +0100 Original-Received: from localhost ([::1]:41057 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ePTy6-0002Er-Kg for guile-user@m.gmane.org; Thu, 14 Dec 2017 08:54:34 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:45089) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ePTuy-0000XP-J8 for guile-user@gnu.org; Thu, 14 Dec 2017 08:51:21 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ePTuu-0003Lz-KC for guile-user@gnu.org; Thu, 14 Dec 2017 08:51:20 -0500 Original-Received: from mail-pf0-x230.google.com ([2607:f8b0:400e:c00::230]:45223) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1ePTuu-0003Kt-Ew for guile-user@gnu.org; Thu, 14 Dec 2017 08:51:16 -0500 Original-Received: by mail-pf0-x230.google.com with SMTP id u19so3634196pfa.12 for ; Thu, 14 Dec 2017 05:51:16 -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=4Rz6eQug2VEH2oqOSMiono+r+0Qam/1/ehVX0d/iM+8=; b=n26ODs0YNqcjQqTn4Cy1RL8fNQRYiwd/ZlhoXuOq4POhjF0w2z+iBtrga8aJ52j8rz 0FBwwxoff+kksdq/YV0zPXDci80A9IcidEWlYWYwoqS0FZ7PJzQni0YeUU1pp4AtPGdR FbKriVSQT7AZ6seXa/dHSqzDZ+6tRYBkvYtYbDuSlb91JrDdZQUnF+swApaiPv8Tz/bl mJwEcEHoxa3FC5hr8IPf61bXJP2vRJHGz/N0jaofeKwVO1dWqdwpniCcfW91KfGHRIk7 c4P2xwTfHfXgwRHsBkJIzo6+49gNDnEM6laQxH/qnZKLeaR/gxFMuldCkVLpJBvIKiiy L+og== 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=4Rz6eQug2VEH2oqOSMiono+r+0Qam/1/ehVX0d/iM+8=; b=eFXaChB0hzIk7eL2zaOPN4Bmq+GDxUhg6Sl/8UfgUXUNGWCX5UUyj1lMmWvuY6JFiM AQRW5+drZufxI5hNvQLCR5lkHEvelo2FNDvX3t/+XwZAZ6G2gguaPxSz08sugxwy5B/E Xuzx8zn40fuBgXzl8SdtXaWm651r5YxlS95thQ5abdPhSdmlIkDoGLj4M2Eo0Jdw2ZE3 +iLf/iGw3lUGX8Ypue9mCdv/OubNPhbnIr3lLLkMxlEMHK+0Tvb6GhwiQauBYet2P+m6 J+Nx0jDQ23UJj4xKsMkMy1da/9MiRGW3HfAPc52PgXZ9aKwP+IYb6j234MHymAHXowhP 5rrw== X-Gm-Message-State: AKGB3mII7avc2o/SNSDvVyiSaMogBxZvEQLb0K85ahsQNUUv48aiHlbR OMZUHZ/eqSaQ29cRSY3LVLE= X-Google-Smtp-Source: ACJfBotnD0kORckDcFKdjYFEEdzTZh3Y/LLb+ljkRrqZSS1iJcnOOPUHWaIP3SsQBCp4pOwzpLdr7Q== X-Received: by 10.99.113.83 with SMTP id b19mr8542127pgn.41.1513259475202; Thu, 14 Dec 2017 05:51:15 -0800 (PST) Original-Received: from debian (n218103180050.netvigator.com. [218.103.180.50]) by smtp.gmail.com with ESMTPSA id p87sm8423471pfi.95.2017.12.14.05.51.12 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 14 Dec 2017 05:51:13 -0800 (PST) In-Reply-To: <876099cx0l.fsf@netris.org> (Mark H. Weaver's message of "Thu, 14 Dec 2017 07:30:18 -0500") X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:400e:c00::230 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:14354 Archived-At: --=-=-= Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Hello Mark, Oh, I didn't really think too much about this. I always thought immediate objects like SCM_EOL, SCM_BOOL_T, ..., SCM_UNDEFINED can be compared using '=3D=3D'. Is this an implementation detail that I should not depend on? Mark H Weaver writes: > 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 = not 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= to represent > all of Guile=E2=80=99s Scheme objects, no matter what the Scheme obj= ect > 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= macros 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= =98SCM=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 --=-=-= Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAEBCgAdFiEEdZDkzSn0Cycogr9IxYq4eRf1Ea4FAloygcMACgkQxYq4eRf1 Ea7S6g//UoQunI9eEnN01RY0SM0q9A1ouKVZnEfPGyOuWlQc/b7xIdlO8ltrzuxd 7d6yb59XLbJvlzVEoa/UxvcW0dde1qqLcJan6EzDhiits3R3YXUfYHq1OZmnxrDR e2hZCOLIQUtqtYHFH9zwhyKTTuP4SDSEqZGEiQf85/cR1dOOHaARhMWybGvuH0El xxtKEETLVyrRzLuZpGLf5dgv+0ofokIzXoSZ+krqJI7F7dGbSf/1BBXlamNIhOeK jLJqw6XD0lNgKUhNLURUdzNW2Wwo1t45eV6BajT/KZP1tHHMwItpv2VU+ssiLoka O+NgHFrQCegKaBdyS0dSgAuBDqzguHPcCKpQP80LVCmPkE1u3tA2T+SBs3GriJ+y m9JUBsWHBScM4WUJ5rA2qLQfyfJmF2Bkn3RVS1+2RZlV+SlbcFSDt28DvR5fgTlv ONYLthLtrQ1Mk2cwcgpMEJIHcOHpwv0O5DFyc00et0mc/CtJgVHc1+ozYNhSbmMo aZLFrnMBkrtISbW3wXoRDOVpYyhVBbFJxqinvnc6mNJdWRSmFaFBBywKyNq1s2RZ LxOqIisbhZcG/ZioklyOpDbPts3UGW0F/ps0iz2lcZVAoTU/i2xKNu0zp23UGleE OxMupTLnxoYiE9sycs5sSIV4pwRVFbyDgKfXgGbY0M3rvSJR6CA= =rjem -----END PGP SIGNATURE----- --=-=-=--