Skip to content

Commit

Permalink
Register the full IDate S3 class for S4 dispatch (#6844)
Browse files Browse the repository at this point in the history
* 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).
  • Loading branch information
aitap authored Feb 28, 2025
1 parent dfb4612 commit b0a17dd
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

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.

## BUG FIXES

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.

# data.table [v1.17.0](https://github.com/Rdatatable/data.table/milestone/34) (20 Feb 2025)

## POTENTIALLY BREAKING CHANGES
Expand Down
3 changes: 1 addition & 2 deletions R/AllS4.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ if ("package:data.table" %in% search()) stopf("data.table package loaded. When d

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

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

methods::setOldClass("IDate")
methods::setOldClass(c("IDate", "Date"))
methods::setOldClass("ITime")

methods::setAs("character", "IDate", function(from) as.IDate(from))
Expand Down
8 changes: 8 additions & 0 deletions inst/tests/S4.Rraw
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,11 @@ test(7.1, is.data.table(DF@x))
# Similar code for under-allocated data.tables in S4 slots, #6704
setClass("DataTable", slots=c(x="data.table"))
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

# IDate was not visible as Date to S4 dispatch, #6839
CustomDurationClass <- setClass("CustomDurationClass", contains = "integer")
setGeneric("%foo%", function(e1, e2) stop("dispatch to default method"))
setMethod(`%foo%`, c("Date", "CustomDurationClass"), function (e1, e2) e1 - e2@.Data)
test(8, as.IDate("2025-03-01") %foo% CustomDurationClass(1), as.IDate("2025-02-28"))
removeGeneric("%foo%")
removeClass("CustomDurationClass")

0 comments on commit b0a17dd

Please sign in to comment.