Skip to content

Commit b0a17dd

Browse files
authored
Register the full IDate S3 class for S4 dispatch (#6844)
* Register the full IDate S3 class for S4 dispatch As recommended by ?setOldClass, register c("IDate", "Date"), not just "IDate". * NEWS entry * Remove setOldClass("data.frame") Instead of defining and exporting an S4 class for data.frame (which we don't own), keep setOldClass(c("data.table", "data.frame")) (which we do).
1 parent dfb4612 commit b0a17dd

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

NEWS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66

77
1. New `sort_by()` method for data.tables, [#6662](https://github.com/Rdatatable/data.table/issues/6662). It uses `forder()` to improve upon the data.frame method and also match `DT[order(...)]` behavior with respect to locale. Thanks @rikivillalba for the suggestion and PR.
88

9+
## BUG FIXES
10+
11+
1. Custom binary operators from the `lubridate` package now work with objects of class `IDate` as with a `Date` subclass, [#6839](https://github.com/Rdatatable/data.table/issues/6839). Thanks @emallickhossain for the report and @aitap for the fix.
12+
913
# data.table [v1.17.0](https://github.com/Rdatatable/data.table/milestone/34) (20 Feb 2025)
1014

1115
## POTENTIALLY BREAKING CHANGES

R/AllS4.R

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ if ("package:data.table" %in% search()) stopf("data.table package loaded. When d
33

44
## Allows data.table to be defined as an object of an S4 class,
55
## or even have data.table be a super class of an S4 class.
6-
methods::setOldClass(c('data.frame'))
76
methods::setOldClass(c('data.table', 'data.frame'))
87

98
## as(some.data.frame, "data.table")
@@ -16,7 +15,7 @@ methods::setAs("data.table", "data.frame", function(from) {
1615
as.data.frame(from)
1716
})
1817

19-
methods::setOldClass("IDate")
18+
methods::setOldClass(c("IDate", "Date"))
2019
methods::setOldClass("ITime")
2120

2221
methods::setAs("character", "IDate", function(from) as.IDate(from))

inst/tests/S4.Rraw

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,3 +118,11 @@ test(7.1, is.data.table(DF@x))
118118
# Similar code for under-allocated data.tables in S4 slots, #6704
119119
setClass("DataTable", slots=c(x="data.table"))
120120
test(7.2, options=c(datatable.alloccol=0L), {DT = new("DataTable", x=data.table(a=1)); DT@x[, b := 2L]; DT@x$b}, 2L) # NB: requires assigning DT to test assignment back to that object
121+
122+
# IDate was not visible as Date to S4 dispatch, #6839
123+
CustomDurationClass <- setClass("CustomDurationClass", contains = "integer")
124+
setGeneric("%foo%", function(e1, e2) stop("dispatch to default method"))
125+
setMethod(`%foo%`, c("Date", "CustomDurationClass"), function (e1, e2) e1 - [email protected])
126+
test(8, as.IDate("2025-03-01") %foo% CustomDurationClass(1), as.IDate("2025-02-28"))
127+
removeGeneric("%foo%")
128+
removeClass("CustomDurationClass")

0 commit comments

Comments
 (0)