www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Calling un-overridden class method

reply "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
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(
				// PROBLEM #1: for some reason, this
				// 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);

			// PROBLEM #1: due to PROBLEM #1, this causes an
			// 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
parent Timon Gehr <timon.gehr gmx.ch> writes:
On 10/13/2012 04:22 AM, H. S. Teoh wrote:
 ...
 			// PROBLEM #1: due to PROBLEM #1, this causes an
 			// infinite recursion that eventually overflows
 			// the stack.
 ...

I'm sure it does :). topic: yes this is a bug.
Oct 12 2012