Inheritance of purity

H. S. Teoh hsteoh at quickfur.ath.cx
Thu Feb 23 15:42:06 PST 2012


On Fri, Feb 24, 2012 at 12:06:19AM +0100, Jason House wrote:
> On Thursday, 23 February 2012 at 17:10:58 UTC, Bruno Medeiros wrote:
> >I would even add to this that it might be useful to have similar
> >syntax that would allow to define an override method without
> >having to specify the return type nor the parameters of the
> >overridden method. Sometimes in class hierarchies there is a lot
> >of redundancy when overriding methods and it could be a nice small
> >feature to reduce that (especially for methods with lots of
> >parameters).
> >
> >class Foo {
> >	int num;
> >	
> >	override opEquals {
> >		if(cast(Foo) o is null)
> >			return false;
> >		return this.num == (cast(Foo) o).num;
> >	}
> >	
> >	override toString {
> >		return to!(string)(num);
> >	}
> >	
> >}
> 
> I don't like omitting argument names, but removing argument types
> seems nice.

Omitting argument names/types is very evil. It opens up the possibility
of changing the base class and introducing nasty subtle bugs in the
derived class without any warning.  For example:

// Original code
	class Helper1 {
		void fun() { writeln("ABC"); }
	}
	class Helper2 {
		void fun() { writeln("DEF"); }
	}
	class Base {
		abstract void gun(Helper1 a, Helper2 b);
	}
	class Derived : Base {
		override void f( /* implicit arguments */) {
			a.fun();	// calls Helper1.fun
			b.fun();	// calls Helper2.fun
		}
	}

// New code
	class Helper {
		void fun() { writeln("ABC"); }
	}
	class Helper2 {
		void fun() { writeln("DEF"); }
	}
	class Base {
		// NB: argument names switched
		abstract void gun(Helper1 b, Helper2 a);
	}
	class Derived : Base {
		override void f( /* implicit arguments */) {
			// SEMANTICS CHANGED WITHOUT WARNING!
			a.fun();	// calls Helper2.fun
			b.fun();	// calls Helper1.fun
		}
	}

Similar problems occur if argument types are switched.


As for removing argument types, we could just use 'auto'. Perhaps the
compiler can interpret 'override auto' to mean 'use base class's return
type' instead of merely 'infer return type from function body'.


T

-- 
"The number you have dialed is imaginary. Please rotate your phone 90 degrees and try again."


More information about the Digitalmars-d mailing list