[chuck-users] Doing something to all members of a class?
Scott Wheeler
wheeler at kde.org
Tue Mar 13 13:03:38 EDT 2007
Kassen wrote:
> Yes, that's it exactly! My reasoning was that it looked to me like
> changing a static variable acted on all members and that this would
> mean that static functions would work in the same way.
>
> This idea got reinforced by Chuck allowing me to adress static
> functions as members of instances and it not being very clear *why*
> this led to issues so I just tried all variations that I could think of.
Yes, that makes sense. I think many ChucK users and certainly the
developers come from a C++ / Java background, so things like the static
keyword are already known.
Probably an even better way of explaining static variables is that
they're basically the same as global variables, just with the class name
there as an organizational convenience. So a static variable is just a
global variable with "Foo." prepended. The same is true for functions.
So, again, examples:
0 => int fooCount;
fun void resetAllFoo()
{
// ...
}
vs.
class Foo
{
0 => static int count;
fun static void resetAll()
{
// ...
}
}
Those are functionally equivalent. The second one makes it possible to
write things in a more tidy Foo.count and Foo.resetAll(), but there's no
difference in what they're able to access. (Note: This is slightly
different from C++ or Java, but it's probably not worth explaining the
subtleties just now.)
Cheers,
-Scott
More information about the chuck-users
mailing list