Mikael Jagan
2025-Dec-27 18:47 UTC
[Rd] Setting of "balanced" attribute by 'length<-.POSIXlt'
Method writers should not go too far to emulate undocumented edge case behaviour of internal default methods. That behaviour could change without notice, leaving those "pedantic" methods out of sync in the end. Notably, here, I would argue that the internal default method is too forgiving. Mikael> Date: Fri, 26 Dec 2025 21:14:09 +0000 (UTC) > From: Suharto Anggono Suharto Anggono <suharto_anggono at yahoo.com> > > I just realized that the default method of 'length<-' in R allowed a string as the assigned length. > > When 'value' is a string, each of > value - length(x) < 1L > and > trunc(value) <= length(x) > give an error, while > value < length(x) + 1 > uses string comparison. > > ----------- > On Saturday, 13 December 2025 at 08:02:28 pm GMT+7, Suharto Anggono Suharto Anggono <suharto_anggono at yahoo.com> wrote: > > > In R 4.5.2 (not before), `length<-.POSIXlt` sets "balanced" attribute of the result to TRUE only if > isTRUE(attr(x, "balanced")) && value <= length(x) . > > For 'value' that is not a whole number, it is possible that value > length(x) and the length of the result is still length(x). > > Instead of > value <= length(x) , > it is better to use > value < length(x) + 1 > (not length(x) + 1L as length(x) may be .Machine$integer.max) > or > value - length(x) < 1L > or > trunc(value) <= length(x) > or > length(r) <= length(x) > or > length(r$year) <= length(x) > (or other component of 'r'). > > By the way, `length<-.POSIXlt` should also use unCfillPOSIXlt(x) instead of unclass(x). For example, when the maximum component length is 3 and a component has length 1 and 'value' (the assigned length) is 2. > > If unCfillPOSIXlt(x) is used and is assigned to 'x', length of any component can be used. > [[alternative HTML version deleted]]