Skip to content

Confusion about indexing behavior #49

@bsdelf

Description

@bsdelf

The document says accessing an index out of bound result in panic

Indexing [], .: array, dict, string, error only. Attempts to access an index out of bounds will result in a panic. Additionally, dicts and errors may be indexed with the dot . operator, as long as the key is a valid identifier.

So I understand following two cases will fail

# case 1
let arr = []
std.print(arr[0]) # panic, index (0) out of bounds
# case 2
let dict = @[]
std.print(dict["a"]) # panic, index ("a") out of bounds

However, if I put the indexing operator on the left hand side, the behavior seems to be inconsistent

# case 3
let arr = []
arr[0] = 1 # panic, index (0) out of bounds
std.print(arr[0])
# case 4
let dict = @[]
dict["a"] = 1 # no panic here, a new key-value pair is inserted
std.print(dict["a"])

It feels like case 4 does not follow the documentation.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions