Skip to content

An inconsistency with promise in attributes

3 messages · Saikat DebRoy, Luke Tierney, Brian Ripley

#
When an attribute is a delayed expression sometimes it is not forced 
when it is extracted.

 > x <- list()
 > attr(x, "p") <- delay(1)
 > x
list()
attr(,"p")
<promise: 0x11e4bb8>
 > val <- attr(x, "p")
 > val
[1] 1
 > attr(x, "p")
<promise: 0x11e4bb8>

I am not quite sure whether the above is a bug or not but I think the 
following is a bug - a promise is supposed to give its value once 
evaluated!

 > eval(attr(x, "p"))
<promise: 0x11e4bb8>
#
On Mon, 11 Aug 2003, Saikat DebRoy wrote:

            
Promises are not forced when retrieving them from a data structure. I
don't think this is a bug (though I don't think the semantics of user
level access to promises are exactly cast in stone).
This should probably be considered a bug in do_eval (internal eval
would force the promise).  I'd be careful fixing it though as it might
break other things.

Promises are really intended to support lazy evaluation and work best
if they are stored as values of variables in environments.  I'm not
sure I would consider other uses reliable in the long run.

Best,

luke
#
On Mon, 11 Aug 2003, Luke Tierney wrote:

            
I noticed that ?delay says

     A _promise_ to evaluate the expression. The value which is
     returned by 'delay' can be assigned without forcing its
     evaluation, but any further accesses will cause evaluation.

and I take that to mean that printing an expression should force promises.
(Not that the help page is definitive, but still I've wondered before why 
print() did not force promises.)

Brian