[chuck-users] Static bug

Hans Aberg haberg at math.su.se
Tue Nov 24 18:16:21 EST 2009


On 24 Nov 2009, at 23:09, Kassen wrote:

>> For some reason, 'chuck' does not instantiate class static  
>> constants until there has been an instantiation of the class itself.
>
> Writing the value to the variable is deemed part of the constructor  
> instead of the instantiation.
> You could get around this using a static member function to set all  
> such values, that way you could have static members set to default  
> values without a class instance.
>
> I'm not completely happy with the current behaviour as two elements  
> of a single line are treated in very different ways without this  
> being intuitively clear. Of course it could be argued that if this  
> bothers us we simply shouldn't do it.

That is my worry, too: lack of intuition. It invites errors.

I wrote code:
   class A {
     ...
     static fun float getr() { return Math.pow(2, l_); }
     ...
   }

This typically causes log(2) to be re-evaluated each time pow(2, l_)  
is computed, assuming that it calls the C pow(). So then introduce a  
value for the constant log(2):

   class A {
     ...
     Math.log(2) => static float log2_;

     static fun float getr() { return Math.exp(l_*log2_); }
     ...
   }

In C++ this is OK. However, in 'chuck', I have to add unrelated code:
   A a;
otherwise the function getr() will not work. Or take the definition of  
log2_ it out of the class A, in which case its name is littering the  
global namespace.

   Hans




More information about the chuck-users mailing list