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

Refactor do_j_names to reuse logic from setDT (#6702) #6802

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

Paligi
Copy link

@Paligi Paligi commented Feb 8, 2025

Hi MichaelChirico,

I’ve been working on refactoring the duplicated logic in the setDT function and the [,:=] operation as discussed in issue #6702.

I have extracted common code into new simple_extract() function.
Here is the code of simple_extract();

simple_extract <- function(name, x, checkSingleChar = TRUE, envir = parent.frame()) {
        k = eval(name[[2L]], envir, envir)
        if (is.list(k)) {
          origj = j = if (name[[1L]] == "$") {
            as.character(name[[3L]])
          } else {
            eval(name[[3L]], envir, envir)
          }
          if (is.character(j)) {
            if (checkSingleChar) {
              if (length(j) != 1L) {
                stopf("Cannot assign to an under-allocated recursively indexed list -- L[[i]][,:=] syntax is only valid when i is length 1, but its length is %d", length(j))
              }
              j2 = match(j, names(k))
              if (is.na(j2)) {
                internal_error("item '%s' not found in names of list", origj)
              }
              j = j2
            } else {
              if (length(j) == 1L) {
                j2 = match(j, names(k))
                if (is.na(j2)) {
                  stopf("Item '%s' not found in names of input list", origj)
                }
                j = j2
              }
            }
          }
          .Call(Csetlistelt, k, as.integer(j), x)
        } else if (is.environment(k) && exists(as.character(name[[3L]]), k)) {
          assign(as.character(name[[3L]]), x, k, inherits = FALSE)
        } else if (isS4(k)) {
          .Call(CsetS4elt, k, as.character(name[[3L]]), x)
        }
      }

Best,
Paligi

- Extract common code into new simple_extract() function.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant