This is the mail archive of the guile@sources.redhat.com mailing list for the Guile project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Returning status from C functions


Ian Grant <Ian.Grant@cl.cam.ac.uk> wrote:
> Hello all,
> 
> Can seasoned Scheme people give their opinions on these two approaches to 
> returning status from C functions.
> 
> I have a library function, func - say, that returns status as an integer.  In 
> the header file, as usual, there is a set of #defines allowing me to  write
> 
>      if (func() != FUNC_OK) .. etc
> 
> When I wrap this for guile, I could either return symbols FUNC_OK, 
> FUNC_FAILED_THIS_WAY, FUNC_FAILED_ANOTHER_WAY etc, or I could scm_sysintern 
> integers like
> 
>     scm_sysintern("FUNC_OK", SCM_MAKINUM(FUNC_OK));
> 
> and have my wrapper return scheme integers.

I can't say I am a seasoned Schemer, but I have used Guile as a
scripting system for my research for several years now. When I have
added C code to the system I have always used if or switch
statements to decode the error code and then throw an error using
scm_error. For example:

   if (errcode != SUBSYSTEM_OK)
        scm_error(gh_symbol2scm("some-error"), "procedure-name",
        "Error description goes here", SCM_BOOL_F, SCM_BOOL_F);

I don't remember the full purpose of the two last arguments, but you
can add SCM values to go into the error description using one/both of
the arguments. 

The advantage with this is that you get nice error handling on the
scheme level. There is a little more of typing of course, but I find
the result more usefule than a simple error code. You might be in
another situation if you are interfacing a system libary or something,
but when you have full control over the error codes, this approach
works like a charm.

        Lars

-- 
Lars Arvestad                               Pharmacia Corp., Stockholm
   Offical email:     Lars.Arvestad@eu.pnu.com  
   Other (preferred): arve@inddama.sto.se.pnu.com

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]