Array Initialization


#1

Quick question…

Are arrays initialized to any particular value at creation - or does it need to be done specifically?

Thanks
Barry


#2

You shouldn’t assume anything about them. They do get initialized to known values but that was really done for me to make debugging the GPScript compiler (yeah, I know it says script but it’s actually a compiler) and even though that initialization is very cheap, I’m reserving the right to change that behavior at any time :grinning:

The problem is that although it might be reasonable that numeric arrays should be 0 and Bools should be false, it’s not obvious what the default midimessage should be (I suppose maybe you could argue for “active sense”) and it’s certainly not obvious what should be in an array of widgets by default.

Dynamic arrays have a variable size that starts at zero and since you can only extend an array by appending to the end, you can’t really get at an array value that hasn’t been explicitly assigned something first.


#3

David,
Many thanks - I never assume these sorts of things - but I thought it worth asking to learn whats really happening under the hood.

So maybe an ‘INITIALIZE_ARRAY’ function would be useful if its more processor efficient than doing it the long way via a loop.

Barry


#4

Not a bad idea though I wonder how one would choose to initialize an array of midievents.


#5

Maybe the initialization should be to a user selectable value - rather than to zero, false and the midievent equivalent (whatever that is) . Thui would make the function generally more useful than a simple array clearing.

Barry


#6

Again, still not obvious. For example, if you’re going to initialize a sequence of midi events, then might it be more useful to have a sequence of increasing notes, or a sequence of increasing CC values for a particular CC number? If not, then what’s the benefit of initialization in advance since you’re going to have to change them all anyway?


#7

I think we are moving away from my suggestion - and that was - was it more efficient processor wise to have a single function that will allow arrays (of whatever type - but obeying the law of common sense) - to be initialized to a single value (rather than in some loop). If you are going to have sequences of midi notes, or arrays of increasing CC values as part of the initialization then that would be very nice - but where does it stop. Actually, I suspect, that the latter should part of the normal script.

Barry


#8

No, I’m trying to push the issue to try and hone in on what’s best from a language perspective. Language design is not a place where one just implements anything at a whim.

Remember that normally you can’t get at the value of an array unless it has already been initialized. Dynamic arrays have bounds checking and you cannot refer to an item at an arbitrary index unless the item has already been appended to the array, thereby increasing its available size.

So given that, it’s unclear as to what is the benefit of pre-initializing arrays, so I’m trying to understand the use case.