Skip to content
Prev 256361 / 398506 Next

best practice(s) for retrieving a local variable from a closure

On Sat, Apr 9, 2011 at 3:27 PM, Barry Rowlingson
<b.rowlingson at lancaster.ac.uk> wrote:
In the case of proto it would be this:


library(proto)
mq <- proto(fun = function(., x) x^.$a)
mq$a <- 2
mq$a # 2
mq$fun(3) # 9

or you could make explicit getter and setter functions though, as
seen, they are not really needed:

mq <- proto(fun = function(., x) x^.$a,
		getA = function(.) .$a,
		setA = function(., a) .$a <- a)

mq$setA(3)
mq$getA() # 3
mq$fun(4) # 64