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>
An inconsistency with promise in attributes
3 messages · Saikat DebRoy, Luke Tierney, Brian Ripley
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. Best, luke
Luke Tierney University of Iowa Phone: 319-335-3386 Department of Statistics and Fax: 319-335-3017 Actuarial Science 241 Schaeffer Hall email: luke@stat.uiowa.edu Iowa City, IA 52242 WWW: http://www.stat.uiowa.edu
On Mon, 11 Aug 2003, Luke Tierney wrote:
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).
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
Brian D. Ripley, ripley@stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595