October 18, 2021, 21:14:09

Author Topic: const arrays may be actually changed  (Read 782 times)

Offline steve64

  • Jr. Member
  • **
  • Posts: 17
const arrays may be actually changed
« 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?




Online jsalai

  • Jr. Member
  • **
  • Posts: 41
Re: const arrays may be actually changed
« Reply #1 on: August 08, 2021, 08:07:38 »
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!

Code: [Select]
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.
« Last Edit: August 08, 2021, 08:27:57 by jsalai »
I won't belong to any organization that would have me as a member.
[Groucho Marx]

Offline steve64

  • Jr. Member
  • **
  • Posts: 17
Re: const arrays may be actually changed
« Reply #2 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".

Online jsalai

  • Jr. Member
  • **
  • Posts: 41
Re: const arrays may be actually changed
« Reply #3 on: August 09, 2021, 10:07:57 »
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 me as a member.
[Groucho Marx]

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal