Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Equivalent environments are seen as different when named #161

Closed
moodymudskipper opened this issue Mar 30, 2023 · 3 comments
Closed

Equivalent environments are seen as different when named #161

moodymudskipper opened this issue Mar 30, 2023 · 3 comments
Labels
bug an unexpected problem or unintended behavior

Comments

@moodymudskipper
Copy link

e1 <- new.env()
e2 <- new.env()
attr(e1, "foo") <- attr(e2, "foo") <- "foo_"
waldo::compare(e1, e2)
#> ✔ No differences

attr(e1, "name") <- attr(e2, "name") <- "name_"
waldo::compare(e1, e2)
#> `old` is <env:name_>
#> `new` is <env:name_>

Created on 2023-03-30 with reprex v2.0.2

@hadley hadley added the bug an unexpected problem or unintended behavior label Apr 7, 2023
@hadley hadley added this to the v0.4.1 milestone Apr 7, 2023
@hadley
Copy link
Member

hadley commented Apr 11, 2023

Named environments are deliberately compared by reference as of #133. I made that change as part of #127 (comment) — waldo compares generally compares environments by values but it compare named environments by reference because these are usually special environments (i.e. empty/base/global and packages environments).

Can you please explain where you saw this problem?

@hadley hadley removed this from the v0.4.1 milestone Apr 11, 2023
@moodymudskipper
Copy link
Author

moodymudskipper commented Apr 11, 2023

Sure, it did happen in artificial cases as you expected.

The package {constructive} prints idiomatic building code from objects, this includes environments with all the caveats that this entails. It uses Waldo to confirm that the generated code is faithful to the object.

# devtools::install_github("cynkra/constructive")
library(constructive)
e1 <- new.env()
e1$x <- 1
attr(e1, "name") <- "my_env"

# identical but uninteresting
construct(e1)
#> constructive::env("0x11bd168a0", parents = "global")

# not identical but equivalent, and interesting 
construct(e1, opts_environment(constructor = "list2env"), check = TRUE)
#> list2env(list(x = 1), parent = .GlobalEnv) |>
#>   structure(name = "my_env")
#> Error in `check_round_trip()`:
#> ! {constructive} couldn't create code that reproduces perfectly the input
#> `original` is <env:my_env>
#> `recreated` is <env:my_env>

Created on 2023-04-11 with reprex v2.0.2

It's important for {constructive} not to choke even on rare cases because we'd like the package to be a robust tool to investigate corrupt or weird objects.

You mentioned already in #152 that you expect regular objects in waldo so this issue might be out of scope. In this case I might be able to special case this with a compare_proxy.environment method defined locally with rlang::local_bindings(.env = .GlobalEnv)

@hadley
Copy link
Member

hadley commented Oct 21, 2024

I'm going to close this issue because I think it is out of scope, and it sounds like you have a work around.

@hadley hadley closed this as completed Oct 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug an unexpected problem or unintended behavior
Projects
None yet
Development

No branches or pull requests

2 participants