From bates at stat.wisc.edu  Tue May 17 18:52:37 2005
From: bates at stat.wisc.edu (Douglas Bates)
Date: Tue May 17 18:54:02 2005
Subject: [Rd] returning an empty list.
In-Reply-To: <428A19D1.8040409@berkeley.edu>
References: <428A19D1.8040409@berkeley.edu>
Message-ID: <428A2155.30900@stat.wisc.edu>

James Bullard wrote:
> I would like to return an empty list from a C function. This I would do as:
> 
> if (file.exists()) {
>    /* do something */
> }
> else {
>    SEXP empty_list;
>    PROTECT(empty_list = NEW_LIST(0));
>    UNPROTECT(1);
>    return empty_list;
> }
> 
> The PROTECT, UNPROTECT lines seemed like overkill to me, but as far as I
> understood the documentation this seemed like the correct usage. It
> seems like I could really just do the following:
> 
> return NEW_LIST(0);
> 
> but I thought I'd better ask first. Thanks in advance, I hope I did not
> miss something in the documentation which describes this.

You can use the short form in this case.  You need to PROTECT an SEXP
whenever you do further operations that may trigger a garbage collection
but in this case there are no such further operations.

Depending on what you are going to do with the returned value it may be
simpler to use

 return R_NilValue;

which returns NULL.


[Rd] returning an empty list.
Douglas Bates