Array constants in LiveCode Builder

Peter TB Brett peter.brett at livecode.com
Thu Jan 7 02:48:38 EST 2016


On 06/01/2016 23:32, Ralph DiMola wrote:
> Nice.... Could it also be used like the function call example below(that
> looks for the second argument to be an array) when calling a procedure that
> expects an array? This would make reading function calls long arguments
> lists(like I use) more readable. I don't use arrays for function/command
> optional arguments because you need to declare the array and then add an
> extra line of code to insert each argument into an array.
>
> Example:
> Put MyFunction(SomeVar,{"cats": 5, "dogs": 23},SomeOtherVar) into MyVar

Yes, that should work fine!

> PS: I know there is a good reason but I got to know... 127?

You asked, so I will explain.  :-)

There are actually *two* types of array literals in LiveCode Builder.

1. When all of the values that go into the array -- and therefore the 
full structure of the array -- are directly specified in the source 
code, the array is a "simple constant".  Simple constants are stored in 
the "constant data" section of the LCB bytecode file, and there's no 
practical limit on how large they can be!  Your example above is a 
simple constant.

2. When some key or value in the array isn't known at compile time, then 
the array has to be constructed "on-the-fly".  Example:

     handler MakeSimpleArray(in pKey, in pValue)
        return {pKey: pValue}
     end handler

At the moment, there's a dedicated bytecode opcode for runtime array 
construction.  Each LCB opcode takes up to 255 registers as arguments. 
The "assignarray" opcode uses up one register argument for the 
destination register (i.e. where to store the constructed array), which 
leaves 254 registers for keys and values.  254/2 = 127.

The dedicated opcode is required because LCB doesn't know how to invoke 
handlers with varying numbers of arguments.  Once the bytecode supports 
that, the "assignarray" and "assignlist" opcodes won't be needed any 
more and will be deprecated.

                                     Peter

-- 
Dr Peter Brett <peter.brett at livecode.com>
LiveCode Open Source Team




More information about the Use-livecode mailing list