qore icon indicating copy to clipboard operation
qore copied to clipboard

Field vs. method resolution

Open tethal opened this issue 8 years ago • 0 comments

When a class has both method and a field with the same name, and the field is of type code, it is not clear which one of the will/should be called.

%new-style
%require-types
%strict-args

class C {
    public {
        code s = sub() {printf("non-static 's' closure called\n");};
        static code s = sub() {printf("static 's' closure called\n");};
    }

    s() {
        printf("C::s() called\n");
    }

    f() {
        s();        # 'calls' non-static field 's'
        C::s();     # calls method s()
        self.s();   # calls method s()
    }
}

C c();
c.f();
c.s();      # calls method s()
C::s();     # 'calls' static field 's'

Output:

non-static 's' closure called
C::s() called
C::s() called
C::s() called
static 's' closure called

Note that there is no way to 'call' the non-static field 's' from outside the class, see #39.

tethal avatar Apr 22 '16 09:04 tethal