r-lib / R6

Encapsulated object-oriented programming for R
https://R6.r-lib.org
Other
410 stars 56 forks source link

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

Open mb706 opened 4 years ago

mb706 commented 4 years ago

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.

wch commented 4 years ago

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:

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

mb706 commented 4 years ago

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