Vectors immutable when used as instance properties?

Here’s a simple program:

[code]#include “tads.h”

MyClass: object {
vec = new Vector([1, 2, 3]);

addToVec(arg) {
    vec.append(arg);
}

}

myObj: MyClass;

main(args)
{
“Before: <<myObj.vec>>\n”;

myObj.addToVec(4);

"After: <<myObj.vec>>\n";

}[/code]

The problem is that when I run it, I get:

However, when I change the line:

vec.append(arg);

to:

vec = vec.append(arg);

… i get the expected behavior, which is:

I’m probably misunderstanding something. I know that Vectors are mutable (unlike lists). However in this case it seems that vectors referenced by instance properties are in fact immutable?

The TADS3 System Manual has a pretty thorough discussion about Vectors at http://tads.org/t3doc/doc/sysman/vector.htm

That is odd, isn’t it? Based on my quick reading of the Vectors page in the System Manual, I would expect that your method would work.

I think that this is something different than you want:

MyClass: object { vec = new Vector([1, 2, 3]);

Operator new means make an allocation now and it’s not a declaration but an expression (programming code to be executed and evaluated) which could exist only inside a function. And TADS is helpful and makes the code into a function, so in reality the above really means:

vec() { return new Vector([1, 2, 3]); }

And that is the problem, every time you use vec as a property somewhere, function is actually called and makes another fresh new vector for you. See Eric’s explanation in recent thread: [url]https://intfiction.org/t/agendaitem-isready-is-a-what/6966/1]. Probably you just want something like this:

MyClass: object { vec: Vector([1, 2, 3]);

(Please note, I actually didn’t tested the code above.)

It’s not valid without new.

I think static is what you need.

MyClass: object { vec = static new Vector([1, 2, 3]); [...] }
See “Static property initialization” at sysman/objdef.htm

Thanks, tomasb. That must be it.

You’re right, it doesn’t work. Apparently you can’t create vectors
like that; you should use new.

Incidentally, one way to initialize a static object’s property with a
vector is, as dddddd shows above, to use static. For
example:

#include "tads.h"

MyClass: object {
    vec = nil;

    addToVec(arg) {
        vec.append(arg);
    }
}

myObj: MyClass {
    vec = static new Vector([1, 2, 3]);
}

myObj2: MyClass {
    vec = static new Vector([5, 6, 7]);
}

main(args)
{
    "Before: <<myObj.vec>>\n";

    myObj.addToVec(4);

    "After: <<myObj.vec>>\n";

    "Before: <<myObj2.vec>>\n";

    myObj2.addToVec(8);

    "After: <<myObj2.vec>>\n";
}

Another way would be to define the static objects with lists instead
of vectors and convert them to vectors at pre-initialization (in a
PreinitObject). I suspect this is the only way if
templates are used for creating the static objects (templates don’t
support passing vectors as properties, only lists).