[chuck-users] Static bug

Hans Aberg haberg at math.su.se
Thu Nov 26 15:06:26 EST 2009


On 26 Nov 2009, at 19:29, Tom Lieber wrote:

> So the idiom I follow is the initialize-after one that's been
> mentioned a few times:
>
> class War {
>  // declarations and instance initialization
>  static int foo;
>  static Gain @ mix;
>  static int bar;
> }
>
> // static initialization
> 3 => War.bar;
> new Gain @=> War.mix;
>
> It's not intuitive to write (until you're used to it), but it is to
> read. It's clear that the static initialization happens once, right
> after class declaration. The static variables can be considered
> initialized anywhere in the class. There's no floating "War war;" to
> get things rolling. If it's a public class, it's likely that this is
> all that's in the file.

I think chuck zero-initializes all stuff, so that would lead to a  
double initialization or introducing a new concept of non-initialized  
variable (as in C).

As "static" does not have any other use outside classes, it seems me  
it can be treated just like introducing namespaces. So
   class A {
     3 => static int k;
     fun static void f() {}
     ... // Non-static stuff.
   }
would be equivalent to
   namespace A {
     3 => static int k;
     fun static void f() {}
   }

   class A {
     ... // non-static stuff
   }

One might introduce "static" as an {...} environment. Then the above  
might be written
   class A {
     static {
       3 => int k;
       fun void f() {}
     }
     ... // Non-static stuff.
   }

This might be a handy way to write static functions: if they are  
global, just move them into the class within a "static {...}"  
construct. And one might put in other global initialization code  
there, if one so likes.

   Hans




More information about the chuck-users mailing list