RobinHankin / disordR

https://robinhankin.github.io/disordR/
1 stars 0 forks source link

unhelpful error message from `unlist()` #48

Closed RobinHankin closed 1 year ago

RobinHankin commented 1 year ago

Look:

> a <- disord(list(1,1:4))
> unlist(a)
Error in .local(x, recursive) : length(out) == length(x) is not TRUE
> 

The error should come from disordR, not wherever that error came from.

RobinHankin commented 1 year ago

The error message does indeed come from the disordR package: unlist() has an S4 method for disord objects which includes the line stopifnot(length(out) == length(x)). I can't quite remember, but I think the intent was to allow this:

> a <- disord(list(1,99,8))
> a
A disord object with hash d78561d4793aa36c10155eff9dfd5c0bae33c405 and elements
[[1]]
[1] 1

[[2]]
[1] 99

[[3]]
[1] 8

(in some order)
> unlist(a)
A disord object with hash d78561d4793aa36c10155eff9dfd5c0bae33c405 and elements
[1]  1 99  8
(in some order)
> 

But to prevent this:

 a <- disord(list(4,1:2,7:9))
> a
A disord object with hash 061e2c0f97b1af712b0ee94c10f2a9938e5c5246 and elements
[[1]]
[1] 4

[[2]]
[1] 1 2

[[3]]
[1] 7 8 9

(in some order)
> unlist(a)
A disord object with hash 061e2c0f97b1af712b0ee94c10f2a9938e5c5246 and elements
[1] 4 1 2 7 8 9
(in some order)
> 

The first call to unlist() is fine: the vector inherits the hash code from the list. But the second is not good disord discipline: the result could have been 4 1 2 7 8 9 or 1 2 4 7 8 9 but not 4 2 1 7 8 9, because the elements of the list are vectors, not disordR objects. Thus the element of a that has length 2 is 1 2 and emphatically not 2 1. There might be a case to return disord(c(4,1,2,7,8,9)) but with a different hash, but to require that recursive is TRUE (and complain otherwise).

In any event, the implemented behaviour is not documented (and is confusing! It confused me just now).