brodieG / diffobj

Compare R Objects with a Diff
229 stars 12 forks source link

Parent named list elements not kept as guides in lists #141

Closed brodieG closed 4 years ago

brodieG commented 5 years ago
library(diffobj)
lm1 <- lm2 <- lm(mpg ~ hp, mtcars)
attr(lm2$qr$qr, 'assign') <- c(1, 0)
diffPrint(unclass(lm1), unclass(lm2))
image

Should preserve one more $qr

brodieG commented 4 years ago

This is because the list print method does not print the parent list element when the child is a classed list, presumably because it does not want to interfere with a potential print method of the child, but I think that's probably bad.

brodieG commented 4 years ago

Indeed, when recursively printing, the global tag buffer gets reset by the print.default method.

A possible solution would be to make the tag buffer reset the responsibility of printValueRec, where it would record on first call that print recursion is active and on exit reset the tag buffer.

There are two obvious issues with this:

  1. The code becomes more complex because in addition to the global recursion state variable, we also need to add a context so we can have an on.exit cleanup in case something along the print recursion triggers an error or similar. There already is a context that is ifdef'ed for win32, so we'd have to combine that handling. Not sure what the performance implications of that are.
  2. If the author of a print method for an S3/S4 object relies internally on the default print methods, they will expect the tag buffer to be reset for them. This means we need a way to recognize that print.default was called internally by the C print recursion code, vs by a the owner of an object print method.
brodieG commented 4 years ago

See #146 for a possible future work-around.

Also Lionel's issue on bugzilla, and discussion on r-devel.