digitalmars.D.learn - Calling un-overridden class method
- H. S. Teoh (43/43) Oct 12 2012 Today I ran into a bit of a bind. I have a class hierarchy in which a
- Timon Gehr (3/8) Oct 12 2012 I'm sure it does :).
Today I ran into a bit of a bind. I have a class hierarchy in which a base class B defines a method eval, which returns a forward range struct whose save method consists of a delegate that simply re-invokes eval with the same arguments. Then there's a derived class C, which overrides B.eval, but uses B.eval as part of its implementation of C.eval. The problem is, I can't seem to specify that I want it to _statically_ bind the save method to call B.eval; even though I specify B.eval explicitly, it still ends up in C.eval, thus causing infinite recursion: class B { // This is a forward range static struct ResultRange { ResultRange delegate() saveImpl; property auto save() { return saveImpl(); } ... // other range methods } ResultRange eval(T[] args) { return ResultRange( // calls C.eval(), even though B.eval is // explicitly specified! /* this is saveImpl */ () => B.eval(args), ... ); } } class C : B { ResultRange eval(T[] args) { auto orig_range = super.eval(args); // infinite recursion that eventually overflows // the stack. auto saved_range = orig_range.save; auto modifiedRange = ...; return modifiedRange; } } Why does D still generate a dynamic call to the overridden eval method, even though I explicitly asked for B.eval? How do I get a static binding to B.eval? Is there a way to work around this? Perplexed, T -- PNP = Plug 'N' Pray
Oct 12 2012
On 10/13/2012 04:22 AM, H. S. Teoh wrote:... // infinite recursion that eventually overflows // the stack. ...I'm sure it does :). topic: yes this is a bug.
Oct 12 2012