vctrs icon indicating copy to clipboard operation
vctrs copied to clipboard

Convenience promotion type from character to dates

Open lionel- opened this issue 5 years ago • 6 comments

There should be a convenience common type so that this is allowed:

x < "2000-01-01"

On the other hand, directional casting should probably only be allowed towards dates. This should be an error:

map_chr(1:3, ~ Sys.Date())

Another type that has a convenience promotion type is version.

lionel- avatar Mar 28 '20 09:03 lionel-

This would imply cast methods should not be more permissive than common type methods, but they can be more restricted.

lionel- avatar Mar 28 '20 10:03 lionel-

@hadley points out this may be only relevant for comparisons, not general combinations.

lionel- avatar Mar 31 '20 06:03 lionel-

May still be useful for general combinations. On R 4.1 concatenating dates in this way will cause promotion of character to dates:

c(Sys.Date(), "2020-02-02")

which seems to make sense from a user point of view.

lionel- avatar May 04 '20 13:05 lionel-

I currently hack around this in almanac for character -> Dates https://github.com/DavisVaughan/almanac/blob/9767f9341f044a95ed900a9f1c0777f03c05dd82/R/utils.R#L20-L44

DavisVaughan avatar May 19 '20 19:05 DavisVaughan

And my hack around this for character -> datetime. Mostly taken from vctrs 0.2.4 😅

https://github.com/rstudio/connectapi/blob/00a01aa74aee8df5fcc67cee14b80c5239168b52/R/parse.R#L121-L129

colearendt avatar May 20 '20 13:05 colearendt

@colearendt I think your function should have "construct" or "new" in the name rather than "coerce". Also it should probably not call stop_incompatible_cast(), this stopper is meant to be called from cast methods.

lionel- avatar May 20 '20 15:05 lionel-