cynkra / constructive

Display Idiomatic Code to Construct Most R Objects
https://cynkra.github.io/constructive
Other
131 stars 6 forks source link

Error recreating igraph object #473

Closed krlmlr closed 3 months ago

krlmlr commented 3 months ago
options(conflicts.policy = list(warn = FALSE))

library(igraph)
g <- make_ring(10)
constructive::construct(V(g))
#> Error in `constructive::construct()`:
#> ! {constructive} could not build the requested code.
#> Caused by error in `.cstr_construct.environment.env()`:
#> ! could not find function ".cstr_construct.environment.env"

Created on 2024-08-22 with reprex v2.1.0

moodymudskipper commented 3 months ago

This was a typo in an edge case hard to test, some environments are created with a NULL parent, though according to the doc NULL parents are not supported anymore.

The current code only supports ".env" for the latter because the parent can't be set, but this could be improved since some constructors don't respect the parent anyway. Keep me informed if you need need this feature.

krlmlr commented 3 months ago

Thanks. According to dput() this is a "weakref" . I was just surprised that constructive yelled at me, no particular action is necessary now.

moodymudskipper commented 3 months ago

yes there was a weakref, but weakrefs are built by rlang::new_weakref on top of environments or external pointers (returned by rlang::wref_key(x)), and in that case this environment had a NULL parent.

.cstr_construct.weakref.new_weakref <- function(x, ...) {
  args <- list(rlang::wref_key(x))
  # assigned this way so no element is added if NULL
  args$value <- rlang::wref_value(x)
  code <- .cstr_apply(args, "rlang::new_weakref", ...)
  repair_attributes_weakref(x, code, ...)
}