Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't allow 1:length(vector) #27

Open
Dasonk opened this issue Jul 17, 2017 · 2 comments
Open

Don't allow 1:length(vector) #27

Dasonk opened this issue Jul 17, 2017 · 2 comments

Comments

@Dasonk
Copy link

Dasonk commented Jul 17, 2017

Strict produces an error when one uses something that results in 1:0. The goal here being to prevent cases where someone uses 1:length(x) which can produce 1:0 when x is empty.

It seems to me the strict package is intended to help catch errors before they actually occur. If that's the case then it should warn about any uses of code that looks like 1:length(x) regardless of if it produces 1:0 or 1:10.

@bbolker
Copy link

bbolker commented Aug 4, 2017

make 'em use seq()! (this would also prevent the 1:n-1 == 0:(n-1) operator-precedence confusion ...)

@torfason
Copy link

torfason commented Aug 27, 2020

make 'em use seq()! (this would also prevent the 1:n-1 == 0:(n-1) operator-precedence confusion ...)

seq() is no better:

seq(1,0)
#> [1] 1 0

I typically include a function I once created and called safer_seq() with a similar purpose as the strict functions:

function (from, to) 
{
    stopifnot(round(from) == from)
    stopifnot(round(to) == to)
    stopifnot(to >= from - 1)
    return(seq_len(1 + to - from) + from - 1)
}

Perhaps a strict_seq() should be added to strict?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants