YetAnotherForum
Welcome Guest Search | Active Topics | Log In | Register

Problem with classes and tables
karmbzk
#1 Posted : Saturday, July 2, 2016 1:45:39 PM(UTC)
Rank: Newbie

Groups: Registered
Joined: 5/26/2013(UTC)
Posts: 6

Thanks: 0 times
Was thanked: 0 time(s) in 0 post(s)
I have a problem with tables. I have created two objets and when I change the table of one of this, it is changed in the other object too...
I don't know how to solve this.

Code:

class Driver{
    name = 0;
    times = {};

    constructor(n){
        this.name = n;
    }
    
    function AddTime(time){
        this.times[this.times.len()] <- time;
    }
    
    function GetTimes(){
        return this.times;
    }
}

local d1 = Drive("D1");
local d2 = Drive("D2");
log("times.len() " + d2.GetTimes().len());
d1.AddTime(12345);
log("times.len() " + d2.GetTimes().len());


Before the line "d1.AddTime(12345);" the length of the table of the d2 object is 0,
but after the instruction the length is 1, the same of the d1 object...
absence
#2 Posted : Friday, July 8, 2016 10:14:06 PM(UTC)
Rank: Advanced Member

Groups: Registered
Joined: 8/23/2014(UTC)
Posts: 107
Man
Location: Northern Germany & Lincolnshire, U.K.

Thanks: 1 times
Was thanked: 10 time(s) in 10 post(s)
honestly, I don't get it. This should not happen.
Zylann
#3 Posted : Tuesday, November 8, 2016 7:46:16 PM(UTC)
Rank: Advanced Member

Groups: Registered
Joined: 6/25/2014(UTC)
Posts: 61
Location: France

Thanks: 1 times
Was thanked: 2 time(s) in 2 post(s)
It happens because `times` is set to an empty table from within the class body, not the constructor. In Squirrel those types are not cloned per instance, so when you create two Drivers they will share the same table.
To fix this, you have to assign the empty table in the constructor.
absence
#4 Posted : Wednesday, November 9, 2016 2:56:00 AM(UTC)
Rank: Advanced Member

Groups: Registered
Joined: 8/23/2014(UTC)
Posts: 107
Man
Location: Northern Germany & Lincolnshire, U.K.

Thanks: 1 times
Was thanked: 10 time(s) in 10 post(s)
Ah, now I get it. Sorry, I'm coding a damn LOT of squirrel, but I seem to intuitively do this right (initializing in code, never in definition)

The exact explanation would be:
though each class instance clones the class definition and has its own member "times", those members are simply a reference to the ONE AND ONLY table inside the class definition.
Or, in other words... In any assignment ( a=b;) the object is RIGHT hand(b) and left hand (a) is always a reference. (unless right hand is a literal/intrinsic value)
And squirrel never clones deep!

same as...
local a={ test="123 } ; //resembles class definition

local b=a ; //assigns b a reference to a!
print (b.test) ;
a.test="pwnd!"; //so that a change of a.test...
print (b.test) ; //...reflects in b!


Another similar pitfall:

function dosomething_with(sometable)
{
local b=sometable ;
b.changed<-true ; //adds that also to sometable ;
b=clone sometable ;
delete sometable.changed ;
//now table doesn't have the slot "changed" anymore, but b still has because it was explicitly cloned
return b ;
}

//note, however:
local demonstration=["Be aware!"] ;
local st={ somedata=1234, someotherdata=demonstration } ;
local test=dosomething_with(st) ;
print(st.someotherdata[0]) ; //will print "Be aware!" as expected
print(test.someotherdata[0]) ; //and this will, too!
demonstration[0]+=" Bugs!";
print(st.someotherdata[0]) ; //will print "Be aware! Bugs!"
print(st.someotherdata[0]) ; //will print "Be aware! Bugs!"







devlin
#5 Posted : Saturday, March 25, 2017 12:30:32 PM(UTC)
Rank: Member

Groups: Registered
Joined: 6/21/2015(UTC)
Posts: 10
Location: Sweden

Thanks: 0 times
Was thanked: 1 time(s) in 1 post(s)
Change your class to this:

Code:

class Driver
{
    name = 0;
    times = null;

    constructor(n){
        this.name = n;
        times = {};
    }
...
Users browsing this topic
Guest
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Clean Slate theme by Jaben Cargman (Tiny Gecko)
Powered by YAF 1.9.4 | YAF © 2003-2010, Yet Another Forum.NET
This page was generated in 0.277 seconds.