Skip to content

Unexpected array access behaviour #699

@Jaspooky

Description

@Jaspooky

Array indexing appears to return inconsistent results when an expression is inlined vs stored in a var and the var used, despite the expression returning a number.

MRE (Playground Link - version 2.0.5):

(
    $arr := ['Hello', 'Hi', 'Hey'];
    $idx := $floor($random() * $count($arr));
    {
        /* Consistently assigns a single string value */
        "working": $arr[$idx],
        /* Inconsistently returns no match, a single string value, or an array of string values? */
        "not_working": $arr[$floor($random() * $count($arr))]
    };
)

I can see a note in the array navigation docs saying

If the square brackets contains a number, or an expression that evaluates to a number, then the number represents the index of the value to select. Indexes are zero offset, i.e. the first value in an array arr is arr[0]. If the number is not an integer, then it is rounded down to an integer. If the expression in square brackets is non-numeric, or is an expression that doesn't evaluate to a number, then it is treated as a predicate.

Given $idx is always a number, it seems sensible to assume the expression should evaluate and be used as a normal array index returning a single value.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions