Skip to content

Conversation

@yuzhengcong
Copy link

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.
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]] == "$") {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

try and match the original style of the code, it's much harder to read when the diff is larger than necessary

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)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in both branches, we do the j2 = match() and if (is.na()) checks -- move them outside checkSingleChar. Try and think through if that applies elsewhere also.

@venom1204
Copy link
Contributor

hi @Paligi
Just checking in—any updates on the testing process? Let me know if you need anything from my side to move things forward.

@MichaelChirico
Copy link
Member

Closing in favor of #7064, which is nearly ready for merge and addresses the same call sites.

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.

3 participants