qore icon indicating copy to clipboard operation
qore copied to clipboard

Unable to call a member closure directly

Open tethal opened this issue 9 years ago • 1 comments

Code:

class A {
    public {
        code f = sub(x) { return x*x; };
    }
}

A a();
a.f(5);

Error:

warning encountered at ./test_crash.q:8
NON-EXISTENT-METHOD-CALL: call to non-existent method 'A::f()'; this call will be evaluated at run-time, so if the method is called on an object of a subclass that implements this method, then it could be a valid call, however in any other case it will result in a run-time exception.  To avoid seeing this warning, use the cast<> operator (note that if the cast is invalid at run-time, a run-time exception will be raised) or turn off the warning by using '%disable-warning non-existent-method-call' in your code
unhandled QORE System exception thrown in TID 1 at 2015-10-08 12:24:47.214587 Thu +02:00 (CEST) at ./test_crash.q:8
METHOD-DOES-NOT-EXIST: no method A::f() has been defined and no pseudo-method <object>::f() is available

Workaround:

Assign the closure to a local variable like this:

A a();
any f = a.f;
f(5);

tethal avatar Oct 08 '15 10:10 tethal

note: works wtih %new-style within the class:

prompt% qore -ne 'class T { public { code x = sub () { printf("hi\n"); }; } constructor() { x(); }} T t();'
hi

but not outside the class:

prompt% qore -ne 'class T { public { code x = sub () { printf("hi\n"); }; }} T t(); t.x();'
warning encountered at <command-line>:1
NON-EXISTENT-METHOD-CALL: call to non-existent method 'T::x()'; this call will be evaluated at run-time, so if the method is called on an object of a subclass that implements this method, then it could be a valid call, however in any other case it will result in a run-time exception.  To avoid seeing this warning, use the cast<> operator (note that if the cast is invalid at run-time, a run-time exception will be raised) or turn off the warning by using '%disable-warning non-existent-method-call' in your code
unhandled QORE System exception thrown in TID 1 at 2016-06-21 11:02:51.274672 Tue +02:00 (CEST) at <command-line>:1
METHOD-DOES-NOT-EXIST: no method T::x() has been defined and no pseudo-method <object>::x() is available

(note the warning and the runtime error)

davidnich avatar Jun 21 '16 09:06 davidnich