Skip to content

Commit 2a835af

Browse files
extraction/setting retains integer64 subclass (#283)
1 parent 673b074 commit 2a835af

2 files changed

Lines changed: 32 additions & 10 deletions

File tree

R/integer64.R

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -910,6 +910,8 @@ position_args_with_int64_to_int_coercion = function(sys_call, eval_frame, skipLa
910910
#' @rdname extract.replace.integer64
911911
#' @export
912912
`[.integer64` = function(x, i, j, ..., drop=TRUE) {
913+
old_class = oldClass(x)
914+
913915
sc = sys.call() # NB: not match.call(), which eats a missing argument in x[1, , 3]
914916
pf = parent.frame()
915917
args = position_args_with_int64_to_int_coercion(sc, pf)
@@ -963,7 +965,7 @@ position_args_with_int64_to_int_coercion = function(sys_call, eval_frame, skipLa
963965
dim(ret) = if (length(newDim)) newDim else NULL
964966
}
965967

966-
oldClass(ret) = "integer64"
968+
oldClass(ret) = old_class
967969
ret
968970
}
969971

@@ -981,9 +983,10 @@ position_args_with_int64_to_int_coercion = function(sys_call, eval_frame, skipLa
981983
ret = withCallingHandlers_and_choose_call(do.call(`[<-`, c(list(x=x), args)), c("[<-", "[<-.integer64"))
982984
} else {
983985
args$value = as.integer64(value)
986+
old_class = oldClass(x)
984987
oldClass(x) = NULL
985988
ret = withCallingHandlers_and_choose_call(do.call(`[<-`, c(list(x=x), args)), c("[<-", "[<-.integer64"))
986-
oldClass(ret) = "integer64"
989+
oldClass(ret) = old_class
987990
}
988991
ret
989992
}
@@ -996,9 +999,10 @@ position_args_with_int64_to_int_coercion = function(sys_call, eval_frame, skipLa
996999
el = as.integer(el)
9971000
el
9981001
})
1002+
old_class = oldClass(x)
9991003
oldClass(x) = NULL
10001004
withCallingHandlers_and_choose_call({ret = do.call(`[[`, c(list(x=x), args))}, c("[[", "[[.integer64"))
1001-
oldClass(ret) = "integer64"
1005+
oldClass(ret) = old_class
10021006
ret
10031007
}
10041008

@@ -1017,9 +1021,10 @@ position_args_with_int64_to_int_coercion = function(sys_call, eval_frame, skipLa
10171021
withCallingHandlers_and_choose_call({ret = do.call(`[[<-`, c(list(x=x), args))}, c("[[<-", "[[<-.integer64"))
10181022
} else {
10191023
args$value = as.integer64(value)
1024+
old_class = oldClass(x)
10201025
oldClass(x) = NULL
10211026
withCallingHandlers_and_choose_call({ret = do.call(`[[<-`, c(list(x=x), args))}, c("[[<-", "[[<-.integer64"))
1022-
oldClass(ret) = "integer64"
1027+
oldClass(ret) = old_class
10231028
}
10241029
ret
10251030
}
@@ -1059,7 +1064,7 @@ cbind.integer64 = function(...) {
10591064
oldClass(l[[k]]) <- NULL
10601065
}
10611066
ret = do.call(cbind, l)
1062-
oldClass(ret) <- "integer64"
1067+
oldClass(ret) = "integer64"
10631068
ret
10641069
}
10651070

@@ -1077,7 +1082,7 @@ rbind.integer64 = function(...) {
10771082
oldClass(l[[k]]) <- NULL
10781083
}
10791084
ret = do.call(rbind, l)
1080-
oldClass(ret) <- "integer64"
1085+
oldClass(ret) = "integer64"
10811086
ret
10821087
}
10831088

@@ -1096,10 +1101,10 @@ as.data.frame.integer64 = function(x, row.names=NULL, optional=FALSE, ...) {
10961101

10971102
#' @export
10981103
rep.integer64 = function(x, ...) {
1099-
cl <- oldClass(x)
1100-
ret <- NextMethod()
1101-
oldClass(ret) <- cl
1102-
ret
1104+
cl = oldClass(x)
1105+
ret = NextMethod()
1106+
oldClass(ret) = cl
1107+
ret
11031108
}
11041109

11051110
# FIXME no method dispatch for :

tests/testthat/test-integer64.R

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1156,3 +1156,20 @@ test_that("extraction consistent to integer: array[", {
11561156
expect_identical(a32[-1, 2, -c(0, 2:3), drop=TRUE], 5:6)
11571157
expect_identical(a64[-1, 2, -c(0, 2:3), drop=TRUE], as.integer64(5:6))
11581158
})
1159+
1160+
test_that("extraction from subclass retains inheritance structure", {
1161+
x = as.integer64(1:2)
1162+
class(x) = c("subclass", "integer64")
1163+
1164+
expect_identical(x[1:2], x)
1165+
1166+
y = x
1167+
y[2L] = as.integer64(3L)
1168+
expect_identical(y, structure(unclass(as.integer64(c(1L, 3L))), class=class(x)))
1169+
1170+
x1 = x[[1L]]
1171+
expect_identical(x1, structure(unclass(x)[1L], class=class(x)))
1172+
1173+
y[[2L]] = as.integer64(4L)
1174+
expect_identical(y, structure(unclass(as.integer64(c(1L, 4L))), class=class(x)))
1175+
})

0 commit comments

Comments
 (0)