Skip to content
Prev 15344 / 63424 Next

Bug in handling of promises?

I'm working on a function that does adaptive sampling, and I thought
it would be handy to return the function's environment as part of the
result so that I could re-use local variables in a subsequent run.  My
first try didn't work, and it came down to code like this:
+     if (!is.null(prevEnv)) H <- prevEnv$H
+     cat('Evaluate H to get ', H(1), '\n')  
+     return(environment(NULL))
+ }

I thought that evaluating H would force it, so that H would be
available in the environment returned by the function.  But this is
not so:
Evaluate H to get  1
<promise: 012094D8>
Error: attempt to apply non-function

So I tried to explicitly force it:
+     if (!is.null(prevEnv)) H <- prevEnv$H
+     force(H)   
+     return(environment(NULL))
+ }

but this still doesn't work:
<promise: 01206FC0>
Error: attempt to apply non-function

It seems that I need to do an assignment to convert H from a promise
to an evaluated object:
+     if (!is.null(prevEnv)) H <- prevEnv$H
+     H <- H     
+     return(environment(NULL))
+ }
function(x) x^2
[1] 1

Is this a bug, or just the way things are?

I get the same results in both R-patched and R-devel.

Duncan Murdoch