return from nested function?
On Wed, 2 Mar 2005 12:13:17 +0000, "Jan T. Kim" <jtk at cmp.uea.ac.uk> wrote :
On Tue, Mar 01, 2005 at 11:21:44PM -0800, Seth Falcon wrote:
On Feb 25, 2005, at 12:34 PM, jhallman at frb.gov wrote:
Is is possible from within a function to cause its caller to return()?
This snippet may be of interest:
f = function(x) {
+ print("f")
+ g(return())
+ print("end of f")
+ }
g=function(x) {print("g")
+ x
+ print("end of g")
+ }
f(1)
[1] "f" [1] "g" NULL
I may be dumb today, but doesn't that beg the question of how does g cause f not to return?
I'm not suggesting that I would ever use code like this, but if x is never evaluated then the double return would not happen:
f
function(x) {
print("f")
g(x, return())
print("end of f")
}
g
function(x, blastoff) {
print("g")
if (x) blastoff
print("end of g")
}
f(TRUE)
[1] "f" [1] "g" NULL
f(FALSE)
[1] "f" [1] "g" [1] "end of g" [1] "end of f" However, this is not a style of programming that I would expect to last for more than 5 minutes after I published a program using it. Follow Thomas's advice, and use tryCatch instead. Duncan Murdoch