You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@lucy.apache.org by Marvin Humphrey <ma...@rectangular.com> on 2014/07/03 07:24:47 UTC
[lucy-dev] Final classes with inherited methods
Greets,
Right now, if a final class inherits a method, we make the final class's
method invocation an alias for the implementing function -- avoiding vtable
dispatch. I have come to believe that this is incorrect.
Consider a parcel com.example.stuff which publishes classes Foo and FooJr.
Foo implements Say_Hello().
public class Foo inherits Clownfish::Obj {
...
public void
Say_Hello(Foo *self);
}
public class FooJr inherits Foo {
...
}
Now, consider another parcel com.example.morestuff which publishes the final
class MyFinalFoo.
public final class MyFinalFoo inherits FooJr {
...
}
We'll get the following method definition for Say_Hello():
#define MORESTUFF_MyFinalFoo_Say_Hello(self) \
STUFF_Foo_Say_Hello(self)
What happens if a newer com.example.stuff is loaded with a FooJr which
implements Say_Hello()? Invocations on MyFinalFoo will invoke the wrong
function: the old one in Foo rather than the new one in FooJr.
I believe that we can only avoid vtable dispatch for methods which are known
to be final because they are explicitly declared as final or because they are
contained within a final class.
(I figured this out because we have an invocation of Hash_Is_A() in
IndexReader.c, and there was a symbol export problem with CFISH_Obj_Is_A_IMP
when I declared that Hash was final.)
Marvin Humphrey
Re: [lucy-dev] Final classes with inherited methods
Posted by Nick Wellnhofer <we...@aevum.de>.
On 03/07/2014 07:24, Marvin Humphrey wrote:
> What happens if a newer com.example.stuff is loaded with a FooJr which
> implements Say_Hello()? Invocations on MyFinalFoo will invoke the wrong
> function: the old one in Foo rather than the new one in FooJr.
Good catch. I agree with your analysis.
> I believe that we can only avoid vtable dispatch for methods which are known
> to be final because they are explicitly declared as final or because they are
> contained within a final class.
We could also call a method directly if a class is declared as final and the
method is defined somewhere in the same parcel. It's probably not a great
optimization in the general case but it would make sense for the Clownfish parcel.
Nick