[Mono-devel-list] overriding return type

Jacob Eggleston jacobmono at yahoo.com
Fri Jul 25 08:46:41 EDT 2003


I'm sure there are better experts on OO programing
here that can explain better, but here's why I don't
believe this is conceptually provided for in OOP. If I
have a peice of code that knows about example2, I can
safely test to see if the return value from the
overrided method() can be upcast to example2.
Conversely, if I have a peice of code that has a
reference to an example2 object (downcast as
example1), and calls method() on that object it would
get back an example2 object, causing an error.

for example, expanding on your example code:

public example1 MyExample;
public void ProcessExample()
{
	// this would result in a "cannot implicitly cast"
error if the 
	// return value is of type example2
	example1 NewExampleObject = MyExample.method();
}

Setting MyExample to a downcast example2 from some
external code works fine. But when the ProcessExample
method calls method(), your example code would return
an example2 object. I believe implicit casts are
allowed in VB, but not in C#, resulting in an error
when the program tries to asign a return value of
example2 to a variable of type example1.

Regards,
Jacob Eggleston

--- Alan Jenkins <Alan.Jenkins at phonecoop.coop> wrote:
> 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?
> _______________________________________________
> Mono-devel-list mailing list
> Mono-devel-list at lists.ximian.com
>
http://lists.ximian.com/mailman/listinfo/mono-devel-list


__________________________________
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