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
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
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
I like the new programming paradigm perspective! Lovingly embracing Chuck-isms... :) On 23/10/2014 6:01 pm, Spencer Salazar wrote:
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
mailto: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 mailto:chuck-users@lists.cs.princeton.edu https://lists.cs.princeton.__edu/mailman/listinfo/chuck-__users https://lists.cs.princeton.edu/mailman/listinfo/chuck-users
_______________________________________________ chuck-users mailing list chuck-users@lists.cs.princeton.edu https://lists.cs.princeton.edu/mailman/listinfo/chuck-users
participants (2)
-
Gonzalo
-
Spencer Salazar