From ggrothendieck at gmail.com  Sat Jan 12 17:02:04 2013
From: ggrothendieck at gmail.com (Gabor Grothendieck)
Date: Sat, 12 Jan 2013 11:02:04 -0500
Subject: [Rd] Tricking Promises into Sending Info Via Args into Caller
Message-ID: <CAP01uRnT_Lvw_97GAhXP7xOo6UjsXCTEVPf-H=OAQfWqwd0h1Q@mail.gmail.com>

The is.pos function below results in the variable, out, being set to
TRUE if the first argument to is.pos is positive and to FALSE
otherwise.

It does this without using the return value or using scoping tricks to
reach into the caller.  Instead it tricks the promise into
communicating one bit of information upwardly from the function to its
caller via the second argument.

One would have thought this to be impossible.  Is this intended behavior?

is.pos <- function(i, x) { if (i > 0) x; NULL }

# in this example actual arg1 of is.pos is positive
out <- FALSE
is.pos(1, out <- TRUE)
out # TRUE

# in this example actual arg1 of is.pos is negative
out <- FALSE
is.pos(-1, out <- TRUE)
out # FALSE

--
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com



[Rd] Tricking Promises into Sending Info Via Args into Caller
Gabor Grothendieck