guile-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: function registered with scm_c_define_gsubr: how can i handle an opt


From: Alex Vong
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
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/25.3 (gnu/linux)

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 '=='. Is this an implementation detail that I should not depend
on?

Mark H Weaver <address@hidden> writes:

> Hi Alex,
>
> Alex Vong <address@hidden> 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

Attachment: signature.asc
Description: PGP signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]