KSP-KOS/KOS

Using Lexicon key as suffix, with array index as well, messes up.

Dunbaratu opened this issue · 2 comments

set myList to LIST(100, 200, 300).
set myLex to LEXICON.
myLex:add("somekey", myList).
// The following *should* replace the 100 in myList with a 1:
set myLex:somekey[0] to 1.
// But instead it gives an error

The error it gives is this:

Strings are imutable; they can not be modiied using the syntax "SET string[1] to "a", etc.

In this context, it appears to be interpreting somekey[0] as the zero-th character of the string "somekey", rather than what it should do, which is get the value of myLex["somekey"], finding it's value is a LIST(), and then changing the LIST's zero-th thing, not the key name's zeroth thing.

Note, you can do print ship:parts[0] and it doesn't trigger the bug. It understands not to eval the [0] until it's done evaluating ship:parts At this point it's unclear if this difference is because :parts is a built-in suffix rather than a user-defined lexicon key, or if it's because ship:parts[0] is being used in a GET context rather than a SET context.

(side note, spelling error in the error message: imutable --> immutable.)

In addition.
Parenthesising the suffix access does not throw an exception but does not do the expected thing either.

// Expected to replace the 100 in myList with a 1:
> set (myLex:somekey)[0] to 1.
// Expected to print 1
> print myList[0].
100 // actual output
// Instead, the key 0 is set in the lexicon
> print myLex[0]
1