[Rd] Recycling in arithmetic operations involving zero-length vectors

GILLIBERT, Andre Andre@G||||bert @end|ng |rom chu-rouen@|r
Mon Jan 16 16:53:25 CET 2023


Duncan Murdoch <murdoch.duncan using gmail.com> wrote:

> To even do that, we would have to first decide which "cases" should produce a warning.

> Let's say `1 + x` should give a warning when x = numeric(0). Then should `x^2` also produce a warning? Should `x^0.5`? Should `sqrt(x)`?
> Should `log(x)`?


The most probable errors would be in functions taking two arguments (e.g. `+`) and for which one argument has length >= 2 while the other has length 0.

In my experience, most code with accidental zero-length propagations (e.g. typo in data_frame$field) quickly lead to errors, that are easy to debug (except for beginners), and so, do not need a warning.

The only cases where zero-length propagation is really dangerous in my experience is in code using an aggregating function like sum(), all() or any(), because it silently returns a valid value for a zero-length argument. Emitting warnings for sum(numeric(0)) would probably have too many false positives but a (length >= 2) vs (length == 0) warning for common binary operators could sometimes catch the issue before it reaches the aggregating function.

-- 
Sincerely
André GILLIBERT


More information about the R-devel mailing list