From a stylistic perspective, the requirement to use two different variables does not seem to add much/any value here: the OP’s use of “w” and “x” avoids circular references (during the S3 approach), but that is not required with the active bindings. ]]>

makeActiveBinding (“ix”, function() x<<-x+1, .GlobalEnv)

]]>

makeActiveBinding("x",

local({.x <- 0; function(z) if (missing(z)) (.x <<- .x + 1) else (.x <<- z)}),

env=.GlobalEnv)

x

# [1] 1

x

# [1] 2

{ x; cat('hello\n'); x; cat('bye\n'); }

# hello

# bye

x

# [1] 5

x <- 11

x

# [1] 12

To store the next value (not-incrementing), just assign it:

a <- x

a

# [1] 13

x

# [1] 14

x

# [1] 15

a

# [1] 13