[Rd] Setting of "balanced" attribute by 'length<-.POSIXlt'

Mikael Jagan j@g@nmn2 @end|ng |rom gm@||@com
Sat Dec 27 19:47:53 CET 2025


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 using 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 using 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]]



More information about the R-devel mailing list