From hpages at fredhutch.org  Fri Jan  9 20:56:50 2015
From: hpages at fredhutch.org (=?UTF-8?B?SGVydsOpIFBhZ8Oocw==?=)
Date: Fri, 09 Jan 2015 11:56:50 -0800
Subject: [Rd] RFC: getifexists() {was [Bug 16065] "exists" ...}
In-Reply-To: <21678.39962.577881.546665@stat.math.ethz.ch>
References: <54AE6C53.8030702@gmail.com>	<bug-16065-16@http.bugs.r-project.org/bugzilla/>	<bug-16065-16-N2rAUyzVWO@http.bugs.r-project.org/bugzilla/>	<21678.19158.736089.111281@stat.math.ethz.ch>	<OF12891DEC.6A4C5E3F-ON85257DC7.004D34A7-85257DC7.004D34AB@american.edu>
	<21678.39962.577881.546665@stat.math.ethz.ch>
Message-ID: <54B03282.2020504@fredhutch.org>

Hi,

On 01/08/2015 07:02 AM, Martin Maechler wrote:
>
>> Adding an optional argument to get (and mget) like
>> val <- get(name, where, ..., value.if.not.found=NULL )   (*)
>
>> would be useful for many.  HOWEVER, it is possible that there could be
>> some confusion here: (*) can give a NULL because either x exists and
>> has value NULL, or because x doesn't exist.   If that matters, the user
>> would need to be careful about specifying a value.if.not.found that cannot
>> be confused with a valid value of x.
>
> Exactly -- well, of course: That problem { NULL can be the legit value of what you
> want to get() } was the only reason to have a 'value.if.not' argument at all.
>
> Note that this is not about a universal replacement of
> the  if(exists(..)) { .. get(..) } idiom,

FWIW, if(exists(..)) { x <- get(..) } is not safe because it's not
atomic. I've seen situations where exists() returns TRUE but then
get() fails to find the symbol (even if called immediately after
exists()).

After scratching my head for a while I found out that the symbol was
removed by some finalizer function defined somewhere (not on the 
environment exists() and gets() were looking at, of course). And
since garbage collection is triggered between the moment exists() sees
the symbol and get() tries to get it, the finalizer was executed and
the symbol removed.

After that, I started to systematically use x <- try(get(...)) instead
(which is atomic).

Cheers,
H.



[Rd] RFC: getifexists() {was [Bug 16065] "exists" ...}
=?UTF-8?B?SGVydsOpIFBhZ8Oocw==?=