TheGameCreators / AGK-Studio

3 stars 1 forks source link

[Request] Correct Arrays to eliminate extra memory waste and fix it to start at 0 or 1 #1096

Open XanthorXIII opened 1 year ago

XanthorXIII commented 1 year ago

Currently trying to build a library and I need the use of arrays but I'm seeing where even though if I define an array to say be length of 5, array.length will return a 5, there is still technically 6 elements in the array, even though I had defined the array to only be 5 elements. Most modern Programming languages start with 0 as the first element and for something like the above it would be element 0 - 4. I could technically work with 1 - 5 but then have an extra element taking up that additional memory space. Sure it may not add up if you only use a few arrays here or there, but if you start setting up types that have arrays and those have multidimensional arrays and so forth and so on, you could run into a bunch of wasted memory.

Here is what I am proposing

A. Fix it so that there is no array element 0, all indices for arrays start with 1 eliminating the extra space thus saving on memory B. Fix it so that the indices start at 0 like most modern languages C. Add an Option to allow you to set which Value you want to start at but please eliminate the extra element

The best option might be A for compatibility for most of the developers and would probably require the smallest amounts of code changes.

C would be good for flexibility allowing developers who are used to working 0 as the starting element but still allowing those that wish to use 1 the option to do so

B would require the most changes to code for all the devs

orvillian commented 1 year ago

The reason that arrays behave like this is due to backwards compatibility with DarkBASIC. I agree it's not perfect and in ideal circumstances this would be changed, however, altering it now will likely cause a lot of issues, particularly for those working on large projects. Even option A will be very awkward as a developer might in some instances start with index 0 in some areas of the project and then 1 in others (I have seen this happen on many occasions). Option C strikes me as being the only viable route. Something to consider in the future.

XanthorXIII commented 1 year ago

I definitely understand this is not an easy change. Maybe something to consider for a future iteration of AGK if TGC decides to update it in a couple of years from now.

MedicineStorm commented 1 year ago

Perhaps I am misunderstanding this issue, but I was under the impression the difference between most modern languages and AGK is not the zero- or one-based indices of arrays, but rather that the AGK .Length method returns the index of the last element of the array, but in other languages, .Length returns the total size of the array. I have been using arrays as if option "B" is already implemented. If I need to get the true size of the array, I just use MyArray.Length + 1 since the number of elements = the last element's index + 1.

Operating this way has been fairly trivial to get used to since I just read .Length to be .LastElementIndex. In fact, this is often more convenient, because when I need to iterate through an array, I always need the last element index anyway. In most other languages, I have to subtract 1 from the array's size to get the right index range. However, reworking how array indices operate at this stage would require nearly every AGK user to rewrite every application.

Forgive me if I have totally misunderstood the request.

XanthorXIII commented 1 year ago

So what's the point of .length if it doesn't return the actual length of an array then but the number of elements like if I had used 1 as the starting Index. If you read the documentation for Arrays, this is how that property is being treated as. How about actually giving the number of elements instead of all this run around. And to your point about using it for the last index of an array, how about a property called .lastindex for you to use.

MedicineStorm commented 1 year ago

If you read the documentation for Arrays, this is how that property is being treated as.

Ok, I'm sorry. I was basing my assumptions on this documentation which says

Note that because arrays start at index 0 and allow the size parameter as a valid index, an array defined as "DIM myArray[2]" or "myArray as integer[2]" would have 3 elements (0,1, and 2) but array.length will return "2" to reflect the size parameter that was used to declare it. Therefore an array of length 0 has one element and an empty array will return the length "-1".

I wasn't trying to say that's how it should be.

how about a property called .lastindex for you to use.

I wouldn't complain if that were added, but I don't need it. You're right, it probably should be called .lastindex instead of .length. I just figured adding the following functions would be easier than causing everyone else to have to rewrite their existing code for the sake of bad semantics:

Function GetArraySize(p_Array As Integer[])
EndFunction p_Array.Length + 1
Function SetArraySize(r_Array Ref As Integer[], p_NewSize As Integer)
    p_Array.Length = p_NewSize - 1
EndFunction
XanthorXIII commented 1 year ago

You're right that the Index starts at 0, it's a lot of the documentation that treats it as starting at 1. For now I'm going to work with what's available, but will be designing my code around using an Index of 0 as opposed to 1 so that I don't have wasted array space.

I think for a future iteration of AGK Studio the array issue can be addressed to make Array's more straight forward.