const arrays may be actually changed

Started by steve64, August 07, 2021, 18:40:26

Previous topic - Next topic

steve64

I observed the following unexpected behavior. The instructions:

const table =  [ 1, 2, 3, 4 ]
print table
table[2] = 5
print table

will print:

[ 1, 2, 3, 4 ]
[ 1, 2, 5, 4 ]

Is the "const" keyword ignored for arrays?




jsalai

#1
Quote from: steve64 on August 07, 2021, 18:40:26
I observed the following unexpected behavior. The instructions:

const table =  [ 1, 2, 3, 4 ]
print table
table[2] = 5
print table

will print:

[ 1, 2, 3, 4 ]
[ 1, 2, 5, 4 ]

Is the "const" keyword ignored for arrays?

IMHO, whatever you do with arrays (dim, redim, nodim, seq... etc...), arrays are ALWAYS dynamic.
That indisciplined behaviour is what I REALLY like in SB! It requires sometimes caution!

dim table(3)
const table=[1,2,3,4]
?table
table[2]=5
?table
table << 5
?table
insert table,2,8
?table
delete table,2
?table


prints:
[1,2,3,4]
[1,2,5,4]
[1,2,5,4,5]
[1,2,8,5,4,5]
[1,2,5,4,5]


cheers

PS.
In case that any atempt will be made to change that (or eg. induce new options - like eg. C-style-codes), for my earlier works I keep in reserve a good old version! For example, I made some eforts to create some *.rtf files directly from SB with heavy use of '\' (0x5C) control chars, that is completely messed up in new version(s). IMHO, any news should keep compatibility with olds.
I won't belong to any organization that would have people like me as members.
[Groucho Marx]

steve64

IMHO, if a programming language allows syntax X, then X must be coherent with expectations.
As alternative, the parser should emit an error like "const invalid for arrays".

jsalai

Quote from: steve64 on August 08, 2021, 22:03:22
IMHO, if a programming language allows syntax X, then X must be coherent with expectations.
As alternative, the parser should emit an error like "const invalid for arrays".

You are right. Specially in your last statement.

But this dynamic behavior started (if I recall well) from PalmOS, v0.8 (unfortunately my PalmPilot m100 is dead, so I cannot check), and I was happy about it, knowing all the troubles with redim's preserving contents. And SmallBASIC was the only useful thing on that "mobile".
Although I use other progs/compilers, I somehow was tied to this gem...
And the further developement PalmOS -> DOS -> Win -> Android (another mobile) tied me even stronger.
So I can 'forgive' any inconsistency, knowing its hystory...

Cheers
I won't belong to any organization that would have people like me as members.
[Groucho Marx]