You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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 1let arr = []
std.print(arr[0]) # panic, index (0) out of bounds
# case 2let 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 3let arr = []
arr[0] = 1 # panic, index (0) out of bounds
std.print(arr[0])
# case 4let 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.
The text was updated successfully, but these errors were encountered:
I think we should update the documentation, as dict["a"] on the left-hand side means insertion (for dicts at least). The current behavior is consistent with other languages such as Python and Lua:
(Python)
The document says accessing an index out of bound result in panic
So I understand following two cases will fail
However, if I put the indexing operator on the left hand side, the behavior seems to be inconsistent
It feels like case 4 does not follow the documentation.
The text was updated successfully, but these errors were encountered: