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

Assigning "this" a cloned copy in a constructor
PoV
#1 Posted : Friday, November 8, 2013 5:05:21 AM(UTC)
Rank: Member

Groups: Registered
Joined: 4/2/2011(UTC)
Posts: 14
Location: Canada

Thanks: 0 times
Was thanked: 0 time(s) in 0 post(s)
Hi,

I've been experimenting with various coding style ideas. One such idea was to try adding a copy constructor.

Is there any reason this shouldn't work?

Code:
class vec2 {
    x = 0.0;
    y = 0.0;
    
    constructor (...) {
        if ( vargv.len() >= 2 ) {
            x = vargv[0];
            y = vargv[1];
        }
        else if ( vargv.len() >= 1 ) {
            if ( typeof vargv[0] == "vec2" ) {
                this = clone vargv[0];
            }
        }
    }

    function _typeof() {
        return "vec2";
    }
};


When I do the following in Squirrel 3.0.4:

Code:
local MyVec = vec2(10,10);
local MyCopy = vec2( MyVec );


I expect MyCopy to be a copy of MyVec, but it's not. If I add a _cloned metamethod, it is never called.


To contrast, this works just fine.

Code:
class vec2 {
    x = 0.0;
    y = 0.0;
    
    constructor (...) {
        if ( vargv.len() >= 2 ) {
            x = vargv[0];
            y = vargv[1];
        }
        else if ( vargv.len() >= 1 ) {
            if ( typeof vargv[0] == "vec2" ) {
                x = vargv[0].x;
                y = vargv[0].y;
            }
        }
    }

    function _typeof() {
        return "vec2";
    }
};


Thoughts?
Mike Kasprzak | @mikekasprzak
fagiano
#2 Posted : Friday, November 8, 2013 1:12:13 PM(UTC)
Rank: Advanced Member

Groups: Registered, Administrators
Joined: 6/11/2005(UTC)
Posts: 1,059

Thanks: 0 times
Was thanked: 79 time(s) in 61 post(s)
Hi,
you cannot do "this = clone x".
I personally do "clone x" if I need a copy, the _cloned meta method is only useful for deep cloning.
here is some code that works:
Code:

class vec2 {
    x = 0.0;
    y = 0.0;
   
    constructor (...) {
        if ( vargv.len() >= 2 ) {
            x = vargv[0];
            y = vargv[1];
        }
        else if ( vargv.len() >= 1 ) {
            if ( typeof vargv[0] == "vec2" ) {
                //this = clone vargv[0];
                //you migh want to do the following
                local other = vargv[0]
                x = other.x;
                y = other.y;
            }
        }
    }

    function _typeof() {
        return "vec2";
    }
    //this is not necessary is useful when you want to do some deep cloning
    function _cloned(other)
    {
        ::print("hi I'm _cloned\n");
    }
    function print()
    {
        ::print(::format("x = %f, y = %f\n",x,y));
    }
};

local A = vec2(666.0,777.0);
local B = clone A;
local C = vec2(A);

A.print();
B.print();
C.print();


ciao
Alberto
Follow me on Twitter @squirrellang
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.067 seconds.