Reassigning a variable


#1

Kind of stumped with the following snippet of ChaiScript code (on version 5.7.0):

class Foo {
    attr x;
    def Foo(x) { this.x = x; }
}

var f = Foo(1);
f = Foo(2);

I’d expect the old object to get discarded and f to take on the newly constructed object. However, this error gets thrown:

Error: "Unable to find appropriate'=' operator." With parameters: (Dynamic_Object, Dynamic_Object) during evaluation at (test.chai 7, 1)

In this case this would be solvable by defining a method like def`=`(other) { this.x = other.x; }. This of course is a constructed test case, I actually have got this problem with reassigning objects of classes registered from C++, and I don’t really know how to (and would rather like not to) implement such an operator in this case.

Is it necessary to implement assignment manually or am I missing something?

– Eric

PS. Thank you for your great work. This library is a joy to work with so far!


#2

After a good night of sleep I played around a bit more, and assigning references instead seems to do what I want. So this works for me:

class Foo {
    attr x;
    def Foo(x) { this.x = x; }
}

var f := Foo(1);
f := Foo(2);

#3

It does seem like a hole though - I think we should want to be able to reassign ChaiScript defined objects when it makes sense to.

I’ll add an issue for it.


#4

FYI, I’ve fixed and closed this issue now.