[Mono-devel-list] overriding return type

Jacob Eggleston jacobmono at yahoo.com
Fri Jul 25 12:55:27 EDT 2003


Well, I stand corrected about implicit downcasts. I
don't have a C# compiler on this machine, so I'm
having to do this from my head - and I've only been
using C# for a few months now.

The thing with OO though, is that in the case you have
specified, you really should still implement the
IList2 class this way:

interface IList2 : IList
{
	IIterator Iterator(); // <- Iterator method acording
to IList
}

When you get an IIterator back from IList2.Iterator()
you should then check to see if it has the modified
functionallity available in IIterator2. In your
example, it's not obvious why, because you only hav
one dirivative of IList. If we add a third, it may be
easier to understand why this is dangerous, and hence
why the compiler doesn't allow it. (At least from my
understanding - like I said, I'm not an expert on the
subject)

interface IList
{
	//get an iterator at the start of the list, i.e.
before the first item
	IIterator Iterator();		
}
interface IIterator
{
	bool Next();		//move onto the next item, false on
failure (EOL)
	object Item		//the current item
	{
		get;
		set;
	}		
}
interface IList2 : IList
{
	IIterator2 Iterator();
}
interface IIterator2 : Iterator
{
	bool Prev();		//move onto the previous item, false on
failure (SOL)
}
interface IList3 : IList
{
	IIterator3 Iterator();
}
interface IIterator3 : Iterator
{
	bool RandomPos();		//move to a random position in the
list
}
class TestList
{
	void Test()
	{
		// This works because IList2 implements IList
		IList list = GetList.NewList();

		// This works
		IIterator iterator = list.Iterator();

		// This causes a casting error because our list is
an IList3 
		// and its Iterator() method returns an IIterator3
object. The 
		// correct thing is for Iterator to return an
IIterator object, and
		// check to see if it can be upcast to IIterator2
		IIterator2 iterator2 = list.Iterator();
	}
}
class GetList
{
	static IList NewList()
	{
		Return new IList3();
	}
}

I think this probably isn't permitted for safety
reasons. Just imagine something the scale of the Mono
class library, the subtle errors that could be
introduced. (I did a quick search for *.cs in the
class folder and turned up about 5,000 cs files!)

Regards,
Jacob Eggleston




__________________________________
Do you Yahoo!?
Yahoo! SiteBuilder - Free, easy-to-use web site design software
http://sitebuilder.yahoo.com



More information about the Mono-devel-list mailing list