[chuck-users] Static attributes and instantiation

Spencer Salazar spencer at ccrma.stanford.edu
Thu Oct 23 03:01:55 EDT 2014


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 at 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 at lists.cs.princeton.edu
> https://lists.cs.princeton.edu/mailman/listinfo/chuck-users
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.cs.princeton.edu/pipermail/chuck-users/attachments/20141023/0683654a/attachment-0001.html>


More information about the chuck-users mailing list