[Mono-devel-list] overriding return type

Alan Jenkins Alan.Jenkins at phonecoop.coop
Wed Jul 23 13:39:13 EDT 2003


I would like to be able to override the return type of a method, to a subclass 
of the previous type, like so:

class example1
{
	virtual example1 method()
	{
		return new example1();
	}
}

class example2 : example1
{
	override example2 method()
	{
		return new example2();
	}
}

(please excuse quality of code, indent etc, due to being newbie)

In my opinion this is a useful feature for an object orientated language - I 
think it would be wrong for "example2.method()" to return an instance of 
"example2" cast to "example1", because code that used these classes might 
have to cast the "example2" instance back to its proper type again, without 
any guarantee that it was actually a casted "example2" instance and not just 
an "example1" instance.

I'm sure that this feature does no harm to a language - "example2" does not 
break the contract expressed in "example1" - and it doesn't complicate the 
language.

I understand that this it is possible to do this with the .NET runtime - that 
you can craft CIL which will do the same as my example - but that the C# 
compiler does not allow it.

Am I mistaken?  Would it be possible to add support for this feature (possibly 
as an command-line option or an option when compiling the compiler)?  Do you 
agree that such a feature is desirable?  Would you be willing to add this 
feature, given that it would create incompatibility with MS's .NET?  Is there 
the will to code it?



More information about the Mono-devel-list mailing list