[Rd] An inconsistency with promise in attributes

Luke Tierney luke at stat.uiowa.edu
Mon Aug 11 14:26:57 MEST 2003

On Mon, 11 Aug 2003, Saikat DebRoy wrote:

> 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

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).

> but I think the 
> following is a bug - a promise is supposed to give its value once 
> evaluated!
>  > eval(attr(x, "p"))
> <promise: 0x11e4bb8>

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.



Luke Tierney
University of Iowa                  Phone:             319-335-3386
Department of Statistics and        Fax:               319-335-3017
   Actuarial Science
241 Schaeffer Hall                  email:      luke at stat.uiowa.edu
Iowa City, IA 52242                 WWW:  http://www.stat.uiowa.edu

More information about the R-devel mailing list