vctrs
vctrs copied to clipboard
Convenience promotion type from character to dates
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.
This would imply cast methods should not be more permissive than common type methods, but they can be more restricted.
@hadley points out this may be only relevant for comparisons, not general combinations.
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.
I currently hack around this in almanac for character -> Dates https://github.com/DavisVaughan/almanac/blob/9767f9341f044a95ed900a9f1c0777f03c05dd82/R/utils.R#L20-L44
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 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.