1. packedarray
Creates a read-only packed array from objects on the stack (Level 2).
1.2. Stack Effects
| Level | Object |
|---|---|
n |
|
… |
… |
1 |
|
0 |
|
| Level | Object |
|---|---|
0 |
|
1.3. Description
packedarray creates a packed array object of length n containing the objects any₀ through anyₙ₋₁ as elements. It:
-
Removes the integer n from the stack
-
Removes n objects from the stack
-
Creates a packed array containing those objects
-
Pushes the packed array on the stack
The resulting object:
* Has type packedarraytype
* Has the literal attribute
* Has readonly access (cannot be modified)
* Is more memory-efficient than regular arrays
The packed array is allocated in local or global VM according to the current VM allocation mode.
1.5. Examples
1 2 3 3 packedarray
% Creates read-only packed array
/add /mul /sub 3 packedarray
% Packed array of operators
42 (text) /name 3 packedarray
% Packed array: <42 (text) /name>
1.6. Common Use Cases
1.6.1. Procedure Definitions
% In Level 2, procedure bodies are packed arrays
true setpacking
{ 1 2 add } % Creates packed array
1.7. Common Pitfalls
Cannot Modify Packed Arrays - Packed arrays are read-only. Attempting to use put or putinterval causes [invalidaccess] error.
|
1 2 3 3 packedarray
dup 0 99 put % Error: cannot modify
Global/Local VM Restrictions - If packed array is in global VM and any element is in local VM, [invalidaccess] error occurs.
|
Use for Procedures - In Level 2, enable packing mode with true setpacking to create packed array procedures automatically.
|
true setpacking
{ add mul } % Automatically creates packed array
false setpacking
Read Operations Work Normally - Can use get, getinterval, aload, forall, etc.
|
1.8. Error Conditions
| Error | Condition |
|---|---|
[ |
Packed array in global VM contains local VM objects |
[ |
n is negative or exceeds implementation limit |
[ |
Fewer than n+1 objects on stack |
[ |
Top operand is not an integer |
[ |
Insufficient VM to allocate packed array |
1.9. Implementation Notes
-
Packed arrays typically use 50-60% less memory than regular arrays
-
Elements are stored more compactly
-
Created with readonly and literal attributes
-
Type is
packedarraytype, distinct fromarraytype -
Can be used anywhere regular arrays are accepted (for read operations)