Re: [R] return from nested function?

From: Duncan Murdoch <murdoch_at_stats.uwo.ca>
Date: Thu 03 Mar 2005 - 01:27:28 EST

On Wed, 2 Mar 2005 12:13:17 +0000, "Jan T. Kim" <jtk@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@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



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Thu Mar 03 02:26:51 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:30:39 EST