Squirrel

The programming language
Welcome to Squirrel Sign in | Join | Help
in Search

class members behave as static.

Last post 03-27-2008, 11:30 AM by fagiano. 3 replies.
Sort Posts: Previous Next
  •  03-13-2008, 9:03 AM 2408

    class members behave as static.

    Hi evryone,
    First, great job :). I've been playing for several days with squirrel, and I find it fantastic ...

    Now, about the problem. The following code
    class Base {
        t = [0];

        constructor()
        {
            ::print(t[0] + " Base constructor\n");
            t[0] ++;
        }
    }

    ::Base();
    ::Base();
    ::Base();
    yields:
    0 Base constructor
    1 Base constructor
    2 Base constructor


    So the table t, is actulay one table shared by all instances of the class. It is not actualy a static member, because if you initialize it in the constructor ( t = [] ) everything works as expexted. Is this the inteded behaviour? I find it a little confusing, and I wasn't able to locate any reference in the documentation.


    A quick search through the code showed, that the prooblem ( if it is really a problem ) is in the instance constructoe - sqclass.cpp line 145:
         for(SQUnsignedInteger n = 0; n < nvalues; n++) {
            new (&_valuesNo [N]) SQObjectPtr(_class->_defaultvaluesNo [N].val);
        }


    When instancing the new object should get complete copy of the defaultvalues and just a reference.


    btw. Squirrel version 2.2. I haven't checked with the older versions.

  •  03-20-2008, 7:27 AM 2411 in reply to 2408

    Re: class members behave as static.

    hmmmm, looks like I haven't been reading cearfully enough .... The issue has already been mentioned by ats in the Roadmap to 3.x thread ...
  •  03-26-2008, 5:38 PM 2424 in reply to 2411

    Re: class members behave as static.

    I just took a look at the 3.0 pre alpha version, and this doesn't seem to be addressed yet. Anyone have a quick fix? This appears to affect all complex types: arrays, tables, instances, etc.
  •  03-27-2008, 11:30 AM 2427 in reply to 2424

    Re: class members behave as static.

    Hi, this topic comes out every few months :)

    I want to make clear, this is not a bug and is the expected behaviour. You must see the value that you assign to fields in a class declaration as a "default value" for the field, is copied verbatim even if is a container type/class instance etc.. . Cloning it at contruction time is not a generic solution. So the contructor is there for you to implement your cloning or creation or whatever you wish.

    FYI : To have a C# style behaviour the class should have some form of hidden pre-contructor function that execute the assignments as a series of statements. This would require a compiler with an AST and a much more complicated construction procedure. I don't plan to do anything similar any time soon(for sure not for squirrel 3.x).

    I hope this clarify the situation

    ciao

    Alberto

View as RSS news feed in XML
Powered by Community Server, by Telligent Systems