Skip to content

"Technically" out-of-range but actually-ok range indexing "unnecessarily" errors #3359

@nsiccha

Description

@nsiccha

Again a somewhat weird title - but as always it comes with an example. The following errors, but I think it should actually be fine.
However, whether or not this errors is probably a decision that can be made arbitrarily.

transformed data {
    vector[2] tmp = [1., 2.]';
    // This is fine and prints `[]`
    print(tmp[2:1]);
    // This is not fine and errors with `vector[min_max] min indexing: accessing element out of range. index 3 out of range; expecting index to be between 1 and 2`
    print(tmp[3:2]);
}

For example, the equivalent in python apparently never errors (which I find weird):

[1, 2][1:0] # fine
[1, 2][2:1] # fine
[1, 2][1:2] # fine?!
[1, 2][2] # not fine

The equivalent in Julia (with the indices shifted due to Julia's 1-based indexing) does behave how I'd like Stan to behave (pretty please):

[1, 2][2:1] # fine, returns []
[1, 2][3:2] # fine, returns []
[1, 2][2:3] # not fine
[1, 2][3] # not fine

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions