I tried let stringArr = newvec(12);
and then attempted to initialize each spot in the array as such: let stringArr!i = newvec(5);
but that returns an error telling me I cannot do that. Is there anyone here who can help me with this dinosaur language?
The let
keyword is used only for creating new local variables (also functions and possibly other things, but that's not really relevant for your question). So, the statement:
let stringArr = newvec(12)
is valid in creating the new variable stringArr
or, more precisely:
stringArr
variable holding the address of that vector.However:
let stringArr!i = newvec(5)
is not valid, because stringArr!i
isn't actually a new variable. It's simply the memory contents of cell number i
in the already existing stringArr
vector.
In other words, the statement:
let stringArr = newvec(12)
creates both the initial pointer cell and the second layer of pointers, the latter of which will not point to anywhere useful yet:
+-----------+ +-------------+
| stringArr | -> | stringArr!0 | -> ?
+-----------+ +-------------+
| stringArr!1 | -> ?
+-------------+
: : :
+-------------+
| stringArr!N | -> ?
+-------------+
And, since those second-layer pointers already exist, you shouldn't be using let
to set them(a). The right way to do what you're trying to achieve is:
let stringArr = newvec(12) // Create new vector AND variable,
// set variable to point at vector.
stringArr!i := newvec(5) // Create new vector, set existing
cell to point at it.
(a) It's similar in C in that you wouldn't write:
int xyzzy[10]; // Make ten-element array.
int xyzzy[0] = 42; // Set first element of array.
In that case, the second line isn't supposed to be defining a new variable, rather its intent is simply to set one of the existing elements to a given value. It should instead be:
int xyzzy[10]; // Make ten-element array.
xyzzy[0] = 42; // Set first element of array.