[chuck-users] setting static data at construction (bug)

Stephen Sinclair radarsat1 at gmail.com
Thu Sep 11 09:41:48 EDT 2008


On Thu, Sep 11, 2008 at 9:27 AM, Kassen <signal.automatique at gmail.com> wrote:
> Stefan Blixt;
>>
>> Yeah, 4 => static int i; in a class should either result in a syntax error
>> or in 4 being assigned to i once, either at define time or the first time
>> the class is referenced. The current behaviour is wrong.
>
> I think it should be set at define time because of the special case that
> static data (in public classes) forms for ChucK. It's useful to be able to
> set values in classes that are never instantiated and are only used to share
> data across the VM. We can already do that by setting the value outside of
> the class in the file that defines the class but I feel this is far cleaner
> and less likely to lead to confusion when we have a lot of files holding a
> lot of classes.

My perspective on this bug is that it should behave like C.  i.e., the
following code,

#include <stdio.h>

int main()
{
    int i;
    for (i=0; i<4; i++) {
        static int count = 10;
        count ++;
        printf("count: %d\n", count);
    }
}

outputs this:

$ ./test
count: 11
count: 12
count: 13
count: 14

Because "count" is only initialized to 10 once, even though it's done
inside the loop.  Because of "static", the initialization assignment
is only executed once, at the time of allocation.

If you remove "static", you get:

$ ./test
count: 11
count: 11
count: 11
count: 11


Steve


More information about the chuck-users mailing list