unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
From: Mark H Weaver <mhw@netris.org>
To: Alex Vong <alexvong1995@gmail.com>
Cc: guile-user@gnu.org, Pierre LINDENBAUM <Pierre.Lindenbaum@univ-nantes.fr>
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	[thread overview]
Message-ID: <876099cx0l.fsf@netris.org> (raw)
In-Reply-To: <877etpwr1c.fsf@gmail.com> (Alex Vong's message of "Thu, 14 Dec 2017 18:19:11 +0800")

Hi Alex,

Alex Vong <alexvong1995@gmail.com> 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 ‘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.
>
> 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 <libguile.h>
>
> static SCM myfun(SCM arg1,SCM opt_arg2)
> {
>   if (opt_arg2 == SCM_UNDEFINED)

You must not use '==' 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 '==' 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
     ‘SCM’ is the user level abstract C type that is used to represent
     all of Guile’s Scheme objects, no matter what the Scheme object
     type is.  No C operation except assignment is guaranteed to work
     with variables of type ‘SCM’, so you should only use macros and
     functions to work with ‘SCM’ values.  [...]

Here's the Guile manual entry for 'scm_is_eq', which also mentions that
you must not use '==':

 -- C Function: int scm_is_eq (SCM x, SCM y)
     Return ‘1’ when X and Y are equal in the sense of ‘eq?’, otherwise
     return ‘0’.

     The ‘==’ operator should not be used on ‘SCM’ values, an ‘SCM’ is a
     C type which cannot necessarily be compared using ‘==’ (*note The
     SCM Type::).

Other than that, the attached code looks good to me.

       Mark



  reply	other threads:[~2017-12-14 12:30 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-12-13 21:50 function registered with scm_c_define_gsubr: how can i handle an optional parameter? Pierre LINDENBAUM
2017-12-14 10:19 ` Alex Vong
2017-12-14 12:30   ` Mark H Weaver [this message]
2017-12-14 13:50     ` Alex Vong
2017-12-15  8:06       ` Mark H Weaver
2017-12-15 16:59         ` Alex Vong

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=876099cx0l.fsf@netris.org \
    --to=mhw@netris.org \
    --cc=Pierre.Lindenbaum@univ-nantes.fr \
    --cc=alexvong1995@gmail.com \
    --cc=guile-user@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).