Please allow: Dim MyArray(-1)

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
Josh
Addict
Addict
Posts: 1183
Joined: Sat Feb 13, 2010 3:45 pm

Please allow: Dim MyArray(-1)

Post by Josh »

Please allow:

Code: Select all

Dim MyArray(-1)
;of course also for ReDim
This would be the same like this:

Code: Select all

Dim MyArray(0)
FreeArray (MyArray())
Background:
I often work with COM functions witch I combine in procedures. The COM arguments are in arrays. Sometimes there are arguments, sometimes there are no arguments:

Code: Select all

Procedure Proc1 (Array TestArray(1))

  Debug "cntParams = " + Str(ArraySize (TestArray()) + 1)
 ;Some COM functions

EndProcedure 

cntParams = 0

;Code needed now
If cntParams = 0
  Dim MyArray(0)
  FreeArray (MyArray())
Else
  Dim MyArray(cntParams - 1)
EndIf

;Code needed in future
;Dim MyArray(cntParams - 1)


Proc1 (MyArray())
Yes, I know, thats only for comfort, but it would make the code more clear
sorry for my bad english
User avatar
charvista
Addict
Addict
Posts: 949
Joined: Tue Sep 23, 2008 11:38 pm
Location: Belgium

Re: Please allow: Dim MyArray(-1)

Post by charvista »

+1
But also, it could be extended to have a range.

Code: Select all

Dim A(-4:4)
meaning from A(-4) to A(4) so it would always have a parallelism with what we need, instead of adjustments.

Arrays with 2 dimensions or more, could be identical:

Code: Select all

Dim A(4:9,1:2)
- Windows 11 Home 64-bit
- PureBasic 6.10 LTS (x64)
- 64 Gb RAM
- 13th Gen Intel(R) Core(TM) i9-13900K 3.00 GHz
- 5K monitor with DPI @ 200%
MachineCode
Addict
Addict
Posts: 1482
Joined: Tue Feb 22, 2011 1:16 pm

Re: Please allow: Dim MyArray(-1)

Post by MachineCode »

Code: Select all

;Code needed now
If cntParams = 0
  Dim MyArray(0)
  FreeArray (MyArray())
Else
  Dim MyArray(cntParams - 1)
EndIf
The above is what ReDim is for. ;)
Microsoft Visual Basic only lasted 7 short years: 1991 to 1998.
PureBasic: Born in 1998 and still going strong to this very day!
Little John
Addict
Addict
Posts: 4779
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Please allow: Dim MyArray(-1)

Post by Little John »

Josh wrote:Please allow:

Code: Select all

Dim MyArray(-1)
;of course also for ReDim
This would be the same like this:

Code: Select all

Dim MyArray(0)
FreeArray (MyArray())
What is the purpose of firstly calling Dim MyArray(0) ?
Just calling

Code: Select all

FreeArray(MyArray())
has the same effect, hasn't it?
Josh wrote:Yes, I know, thats only for comfort, but it would make the code more clear
Since the index -1 would have a special meaning, not consistent with the meaning of all other indexes, this would decrease the readability of the code. In contrast, FreeArray() clearly tells the reader of the code what's going on.
So my vote is -1.
User avatar
Josh
Addict
Addict
Posts: 1183
Joined: Sat Feb 13, 2010 3:45 pm

Re: Please allow: Dim MyArray(-1)

Post by Josh »

MachineCode wrote:

Code: Select all

;Code needed now
If cntParams = 0
  Dim MyArray(0)
  FreeArray (MyArray())
Else
  Dim MyArray(cntParams - 1)
EndIf
The above is what ReDim is for. ;)
What? Have you read my posting? Did you really had a look at my example?
Little John wrote:
Josh wrote:What is the purpose of firstly calling Dim MyArray(0) ?
Just calling

Code: Select all

FreeArray (MyArray())
has the same effect, hasn't it?
Do you know, what you are writing? Apart from the fact, that FreeArray without Dim isn't working, this would break all rules.
Little John wrote:Since the index -1 would have a special meaning, not consistent with the meaning of all other indexes.
There is no special meaning:
Dim MyArray(2) defines an array with 3 elements
Dim MyArray(1) defines an array with 2 elements
Dim MyArray(0) defines an array with 1 element
Dim MyArray(-1) defines an array with 0 elements
Little John wrote:...this would decrease the readability of the code.
If you don't like it, don't use it. For me it would make a better readable code, if I have one line instead of six.
sorry for my bad english
Little John
Addict
Addict
Posts: 4779
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Please allow: Dim MyArray(-1)

Post by Little John »

Josh wrote:
Little John wrote:
Josh wrote:What is the purpose of firstly calling Dim MyArray(0) ?
Just calling

Code: Select all

FreeArray (MyArray())
has the same effect, hasn't it?
Do you know, what you are writing?
In case you want a serious discussion, better stop stupid polemics ...
Josh wrote:Apart from the fact, that FreeArray without Dim isn't working,
You had posted only a short code snippet. I have assumed that the array had been Dimed before somewhere in the code.

Sorry, I was mistaken concerning the effect of FreeArray(), because of its description in the documentation:
Documentation of FreeArray() wrote:Free the specified 'Array()' and release all its associated memory. To use it again Dim has to be called.
I see now that the sentence which I wrote in red here is wrong: After calling FreeArray(), the array can still be used (e.g. as parameter for self-written procedures or as argument for ArraySize()).
Post Reply