Defining size of array with a variable

In Xojo there are a lot of examples of single line DIM that put together several lines:

dim result as new Picture( input.width, input.height ) Dim desktopFolder As FolderItem = SpecialFolder.Desktop Dim f as FolderItem = GetFolderItem ("myFile") etc

So, I can not understand why we must write to lines to define an array with a variable size:

[code]Dim a()
Redim a(i)

Dim b(-1,-1)
Redim b(i,j)[/code]

Would it be so difficult to short it to a single line? For me it is much easier to read.

Dim a(i) Dim b(i,j)

Before using Feedback for a new feature request, I’d like to know your opinion on that.
Wouldn’t you find it useful?

I agree that this (and many, many other small gripes) would be great to add to the language. When I’ve advocated for syntactic sugar like this in the past, the resistance from both Xojo and the user community was surprisingly high. I won’t bore you with details of similar things I’ve asked for, but suffice it to say that the answer was usually along these lines:

  1. Xojo users are generally beginners / novices. Xojo prefers ease of readability over compactness.
  2. The Xojo developers have MUCH bigger fish to fry. Even if they really wanted to add shortcuts like this, it would be several years before they could ever consider doing so.
  3. Stop pestering us. You smell bad.

Now, as for your specific dilemma: Why do you need to specify the size of the array when you instantiate it? I’ve never really been bothered by the issue you face, because I usually deal with arrays like this:

dim arr() as SomeSpiffyType ... arr.append(sst1) ... for i as integer = 0 to ubound(someOtherArrayOfSomeSpiffyType) arr.append(someOtherArrayOfSomeSpiffyType(i)) next ...

You get the idea. I just .append as needed.

I’ve also found that the .append as needed method is a great why to work with dynamic arrays in Xojo. I haven’t once had to dimension an array to a fixed size when declaring it since adopting the .append approach.

Thanks both Kimball and Alwyn.

The truth is I work a lot with numerical matrices in engineering analysis.
These matrices store complex numerical values that must be calculated and entered in very different order, but each value must be placed in its correct way (i,j). That’s the reason I must dimension the array before.

And yes, I know that the benefit or the annoyance is very small, but I can not understand why. It simply I’d like to know it.

[quote=128556:@Ramon SASTRE]Thanks both Kimball and Alwyn.

The truth is I work a lot with numerical matrices in engineering analysis.
These matrices store complex numerical values that must be calculated and entered in very different order, but each value must be placed in its correct way (i,j). That’s the reason I must dimension the array before.

And yes, I know that the benefit or the annoyance is very small, but I can not understand why. It simply I’d like to know it.[/quote]

A feature request never hurts …

For large arrays, pre-dimensioning will be faster, if speed is critical. Also, since Ramon is working with a multi-dimensional array, Append is not an option anyway.

I just did a profiler test on the two different methods for curiosity. It seems that the speed gain is only significant when working with very large arrays (10,000,000+ elements).

For the test I used these two algoritms:

PRE-DIMENSION:

  Dim arr() As Integer
  Dim i As Integer
  
  Redim arr(10000000) // used 1,000,000 for first test, and 10,000,000 for second test
  
  i = 0
  while i < 10000000 // used 1,000,000 for first test, and 10,000,000 for second test
    arr(i) = i
    i = i + 1
  wend

APPEND

  Dim arr() As Integer
  Dim i As Integer
  
  i = 0
  while i < 10000000 // used 1,000,000 for first test, and 10,000,000 for second test
    arr.Append i 
    i = i + 1
  wend

RESULTS:
1,000,000 elements: Pre-dimension = 101 milliseconds, Append = 110 milliseconds
10,000,000 elements: Pre-dimension = 855 milliseconds, Append = 1504 milliseconds

It seems that with very large arrays you can achieve significant speed gains by pre-dimensioning your arrays, while with smaller arrays (< 1,000,000 elements) there isn’t a significant speed gain.

I assume that for small arrays the compiler allocates more space than necessary, so Appends do not take so much time. So when you do

Dim arr(2) As Integer

the compiler creates something like

Dim arr(10) As Integer

internally, while UBound is still 2.

But at a certain point when using Append there is no more consecutive memory available in the heap, because it is taken by other variables. Then the whole array has first to be copied to a new location and where then the next Append can take place.