Hi Gonzalo,

This is either a misfeature, bug, or exciting new programming paradigm, depending on your point of view :)

To explain:
public class Foo {
  1 => static int bar;
}

static int bar; properly declares bar as a static variable. ChucK's "pre-constructor" concept executes all of the actual code in the body of the class every time the class is instantiated. For some reason this includes the 1 => static int bar; which just reduces to 1 => bar; since bar already exists. 

Truthfully this is a bug that should be changed to only set the value once for static vars. In practice I do this:

public class Foo {
  static int bar;
}
1 => Foo.bar;

which achieves the desired outcome with only minor inconvenience. 

spencer



On Wed, Oct 22, 2014 at 7:56 PM, Gonzalo <gonzalo@dense13.com> wrote:
Hi,

This is weird behavior I think:

--------------------
public class Foo {
  1 => static int bar;
}

<<< Foo.bar >>>; // prints 0! Why?

Foo f;
<<< Foo.bar >>>;  // prints 1, ok

2 => Foo.bar;
<<< Foo.bar >>>;  // prints 2, ok

Foo f2;
<<< Foo.bar >>>; // prints 1 again!
--------------------

So it seems that static members get initialized only on instantiation, but to make it funky, on every instantiation...

Is this a buggy implementation of static members, or am I missing something? Not complaining, just a question. :)

Thanks!
Gonzalo
_______________________________________________
chuck-users mailing list
chuck-users@lists.cs.princeton.edu
https://lists.cs.princeton.edu/mailman/listinfo/chuck-users