[chuck-users] accessing static methods through an instance?
Hans Aberg
haberg at math.su.se
Sun Jan 24 19:02:21 EST 2010
On 25 Jan 2010, at 00:22, Robert Poor wrote:
> [Coincidentally, Stefan's message about identically named (instance)
> methods arrived as I was composing this note. It's possible this is
> the same thing, but with a different twist...]
Different but, related: how to best admit name overloading.
> One might expect that an instance reference calls an instance method
> and a class reference calls a static method even if they have the
> same name, but it ain't so. And the compiler doesn't issue any
> warning. Consider the following:
>
> ==== file: fn.ck
> public class F {
> fun static void method() { <<< "static method()" >>>; }
> fun void method() { <<< "instance method()" >>>; }
> }
> F.method(); // call method via class
> (new F).method(); // call method via instance
> ==== produces:
> "static method()" : (string)
> "static method()" : (string)
> ====
>
> I can convince myself that this isn't really a bug, if "static"
> really means "make this variable or method shared by all instances",
> but it would be nice if the compiler warned you about identically
> named methods. (Note that the compiler DOES issue an error for
> identically named variables.)
It is possible to admit it. C++ prohibits it (checking in gcc), though
one calls static and not by rather different syntax:
#include <iostream>
class A {
public:
static int a() {std::cout << "Hi" << std::flush;}
int b() {std::cout << " there!" << std::endl;}
};
int main() {
A::a();
A().b();
return 1;
}
ChucK uses "." where C++ uses "::" for namespaces and "." for methods.
My guess is that C++ prohibits it because the risk of making mistakes.
A matter of taste, thus. But it could still be a bug - only the
developers can tell. :-)
Hans
More information about the chuck-users
mailing list