constructive icon indicating copy to clipboard operation
constructive copied to clipboard

"predefine" as a constructor?

Open krlmlr opened this issue 1 year ago • 3 comments

igraph <- local({
  ..env.1.. <- new.env(parent = baseenv())
  ..env.1..$me <- list(
    3,
    FALSE,
    c(1, 2, 2),
    c(0, 1, 0),
    NULL,
    NULL,
    NULL,
    NULL,
    list(
      c(1, 0, 1), list(name = "Ring graph", mutual = FALSE, circular = TRUE),
      list(), list(foo = c("a", "b", "c"))
    ),
    ..env.1..
  ) |>
    structure(class = "igraph")
  ..env.1..$igraph <- constructive::.xptr("0x0")
  ..env.1..$.__igraph_version__. <- "1.5.0"
  ..env.1..$myid <- "71239ea8-004c-474d-a799-ef174a2d8853"
  ..env.1..$me
})

library(constructive)
# Why not opts_environment("predefine") ?
# Can we reuse stuff that we already put into that environment?
construct(igraph, opts_environment(predefine = TRUE))
#> ..env.1.. <- new.env(parent = baseenv())
#> ..env.1..$.__igraph_version__. <- "1.5.0"
#> ..env.1..$igraph <- constructive::.xptr("0x0")
#> ..env.1..$me <- list(
#>   3,
#>   FALSE,
#>   c(1, 2, 2),
#>   c(0, 1, 0),
#>   NULL,
#>   NULL,
#>   NULL,
#>   NULL,
#>   list(
#>     c(1, 0, 1), list(name = "Ring graph", mutual = FALSE, circular = TRUE),
#>     list(), list(foo = c("a", "b", "c"))
#>   ),
#>   ..env.1..
#> ) |>
#>   structure(class = "igraph")
#> ..env.1..$myid <- "71239ea8-004c-474d-a799-ef174a2d8853"
#> list(
#>   3,
#>   FALSE,
#>   c(1, 2, 2),
#>   c(0, 1, 0),
#>   NULL,
#>   NULL,
#>   NULL,
#>   NULL,
#>   list(
#>     c(1, 0, 1), list(name = "Ring graph", mutual = FALSE, circular = TRUE),
#>     list(), list(foo = c("a", "b", "c"))
#>   ),
#>   ..env.1..
#> ) |>
#>   structure(class = "igraph")
# I would expect a clear error message here:
# construct(igraph, opts_environment("new_environment"))
# Doesn't seem to finish, endless recursion? An error would be fine too:
# construct(igraph, opts_environment("new_environment", recurse = FALSE))

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

krlmlr avatar Jun 11 '23 05:06 krlmlr

Why not opts_environment("predefine")

I think I considered it, but it's not really a constructor, it's just a way to avoid recursion issues, and it breaks the general principle in constructive that we build a single piped call.

and in principle different constructors could be used with the "predefine" behavior, though I'm not so sure how it's working atm.

I would expect a clear error message here: construct(igraph, opts_environment("new_environment"))

Indeed, is it a duplicate of #77 ?

moodymudskipper avatar Jun 11 '23 10:06 moodymudskipper

A call of the form

local({
  ..env.1. <- ...
  ...
})

still can be considered a single piped call. I'm asking because predefine = TRUE overrides all other arguments, so it's a bit weird.

#77 looks similar, yes.

krlmlr avatar Jun 11 '23 12:06 krlmlr

I agree that the current behavior is not satisfying, "predefine" as a constructor makes sense to me in hindsight, I'll give it a bit more thought and fix if I don't find a good counter argument.

moodymudskipper avatar Jun 11 '23 22:06 moodymudskipper