Menu

scScript Unpack Operator: @

While arrays obtained from Sys.GetArgs pack the args passed to the function call, the @ operator can be used to unpack them into another function call.

So if TheFunc is called with:

   TheFunc(MyVar, “This”, 3.14, “That”, 2.718)
   (OtherVar, MyArr[2], MyD.Size, MyD.Weight);

and it later calls:

   Sys.GetArgs()(InArr, InCount, OutArr, OutCount);
   OtherFunc(1, @OutArr);
   AnotherFunc(X, Y, @InArr)(Z, @OutArr);

The call to OtherFunc will be the same as:

   OtherFunc(1, OtherVar, MyArr[2], MyD.Size, MyD.Weight);

Similarly the call to AnotherFunc will appear as:

   AnotherFunc(X, Y,  MyVar, “This”, 3.14, “That”, 2.718)
              (Z, OtherVar, MyArr[2], MyD.Size, MyD.Weight);

Sys.GetArgs will pack its arrays (InArr and OutArr) with the actual variables (parameters) seen inside the caller function (same storage). So setting InArr[0] to Math.#Pi will set the named first in parameter (MyVar) to pi. When unpacking such an array into another function's out list, the very same storage locations are passed to the function. Of course, the in list for the other function is pass-by-value, so it only copies the values, and allocates its own local storage for them. GetArgs uses the same storage, but in lists only accept values!

It is also possible to manually construct arrays and unpack them into function calls at runtime. This is particularly easy for the in list, as the array only needs the values. But things get slightly complicated on the out side, as the called function will be using the very same storage locations as the array elts. It is obviously possible to create and initialize a new array. But elts in such a new array can be set to use the same storage as existing variables using the Alias (=&) operator, outlined in the next section.

scScript can also unpack from a dict. The function being called will see the args in the same order the pairs were initially entered into the dictionary. This is the opposite order to what is actually stored in a dictionary (they are LIFO not FIFO) and reverse of how MapK will traverse the dict, but scScript adjusts for this. However, it is not possible to place a pair on the left of an Alias (=&), as the pair needs more data (pertaining to its own dict) than a regular variable or array elt. So it would be impossible to manually construct a dict that can unpack existing variables into an out list.