Skip to content
Prev 59444 / 63430 Next

Inconsistency of c.Date: non-commutativity and non-integer Value

Hi Jens,
On Fri, Jan 22, 2021 at 1:18 PM Dirk Eddelbuettel <edd at debian.org> wrote:

            
So I think the issue here is twofold. The first (fairly subtle) issue here
is that "Date" is not a type, it's an "(S3) class" (which, in turn, is just
a labeling attribute, as illustrated by the fact that it's removed by
c.default).
[1] "double"


So Date cannot appear anywhere in that hierarchy, because it is a hierarchy
of types.

The reason c(as.integer(1), Sys.Date()) gives a numeric, is in fact because
of that type hierarchy, as one of the elements is a "double" (the Date),
which precludes returning an integer.

The second issue is is that S3 dispatch occurs only on the first element,
so you're hitting completely different methods with  c(as.integer(1),
Sys.Date()) and c(Sys.Date(), as.integer(1)) So that is where your
non-commutativity is coming from.  Personally, I think the case for
c(<Date>, stuff) to give you a Date object back is stronger than that of
commutativity, given the caveat that it would only be commutative if
c(<Date>, <character w/ date string>) gave you a character back and
c(<Date>, <numeric>) gave you a numeric back, but I can see there's space
to disagree about that and argue for commutative absolutism.

Note though that there is unlikely to be a way to get c(<character date
string>, <Date>) to give you a Date back, because, again, S3 dispatch only
sees the first argument, so you would *at best* be in c.character (but in
as it stands now, in c.default, which explicitly strips classes).

I do agree though that imho, the error from the latter could at least be
improved. Unfortunately, Date objects do not carry around their origin, so
we cannot do something such as "use the same origin as the Date object
which caused dispatch into this method for any non-Date objects", which was
going to be my suggestion here until I checked its feasibility.

Best,
~G

|