R6 icon indicating copy to clipboard operation
R6 copied to clipboard

Implement all.equal.R6 to cope with handling of ABs in recent R versions

Open mb706 opened this issue 3 years ago • 2 comments

R-devel has changed their handling of as.list.environment() regarding active bindings. Instead of turning the ABs into functions, it now gives the values that the ABs would have had, had they been accessed normally. This currently breaks all.equal() behaviour in some cases with R6-objects (because all.equal.environment() calls as.list() internally). An example is

> cl <- R6::R6Class("test", active = list(x = function() runif(1)))$new()
> all.equal(cl, cl)
[1] "Component “x”: Mean relative difference: 0.9662922"

(currently only in R-devel). The fact that ABs are expanded to values also makes comparisons very inefficient in cases where the ABs construct large objects for user convenience out of a small amount of data that is more compactly stored in a private$ slot.

I suggest the implementation of all.equal.R6 which should skip active bindings for R6 objects.

mb706 avatar Aug 03 '20 23:08 mb706

It's actually still possible to retrieve the functions for the active bindings, but not with as.list.environment(). There are currently two ways to do it:

  • self$.__enclos_env__$.__active__ is a list containing the functions for active bindings. (But this may go away in a future version.)
  • R 4.0.0 and above have activeBindingFunction(), which gives the function for an active binding.

So an all.equal.R6 method could use of one of these.

wch avatar Aug 04 '20 02:08 wch

I jerryrigged something like this here but it is mostly a quick fix and makes use of data.table to avoid cycles.

mb706 avatar Aug 04 '20 11:08 mb706