Calling un-overridden class method

H. S. Teoh hsteoh at quickfur.ath.cx
Fri Oct 12 19:22:32 PDT 2012


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


More information about the Digitalmars-d-learn mailing list