Assuming, due to the lack of $-prefix that you are trying to make a new, List data type variable, the above does not work. The (optional) type declaration is colon-appended to the var
operator, as documented:
var:list myList = [];
Your example omits the mandatory var
operator so no variable is created. As you are applying a List data type type, the variable initialises as an empty list so the latter declaration is not needed (the default for untyped variables is an empty string. So, this is a shorter form of the above:
var:list myList;
Once declared a variable value can be assigned in either of 3 ways, here for variable myList
:
myList(5);
myList = 5;
The first older form is not much used as people seem to find the latter more intuitive. If myList
is already a List type, the above (re-)set the list’s contents to a single item list of value ‘5’. Within a list—i.e. List or Set type—individual single-value list items, so not nested list or dictionaries, are untyped so @webline’s observation about aTbRef is actually incorrect†.
This is wrong in several ways. As has been stated above, Lists and Sets use zero-based referencding as documented:
The List[N] notation addresses any single List item using a zero-based address.
But using
myList[0]=0;
Doesn’t help as this sets item #0 to 0
. So if the list value was 4;7
the above would change it to 0;7
. For our original example using address #1, the same test would set the list to 4,0
.
So, how to add a new first item? The approved syntax, uses the v9.5.2+ []
list declaration like so:
myList = [0] + myList;
Essentially we join two lists added the second not the first so for the example above the result is 0;4;7
. The first list can ab a list of 1 or more items. The same mechanism is used to add items to to the end of a list:
myList = myList + [0];
As items inside lists have no data type, using the []
declaration for new list items avoids issues of type coercion mentioned in my footnote here.
But, what if you want to insert a new item (single item or list or dictionary) at a specific list address. AFAIK, there is no operator for that. You’ve need to read the parts of the existing list before/after the insertion point into variables and concatenate them with the new list values.
†. The description was correct as originally written though these days ‘must’ might be changed to ‘should’ ‡. the issue surrounds the effects of type coercion when concatenating (+
) differently typed inputs. Also the documentation is written for the general user so doesn’t default to a programmer’s PoV/assumptions (as most users aren’t programmers). Documenting ways coding-savvy users can see to work outside the guidelines (often just to save typing a few extra characters) would simple add confusion. IOW, there may be more than one way to do things but not all are documented—with deliberate good intent.
‡. In fact this is rendered moot by the new []
declarative form. I’ll update the docs.