473,386 Members | 1,962 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Covariant Return Types, programming without them?

I keep running into the scenario below over and over again. Currently I get
around not having covariant return types by using an interface and explicit
property definitions which works to some extent, but causes other problems
that occur with explicit definitions and also leads to much more code that
is also more difficult to maintain. The new keyword is not even an option,
if used in the example below Offspring on a Dog when in the context of an
Animal would return null even the Dog class has populated the Dog _offstring
Array. The new keyword is also extremely difficult to debug when the above
null scenario is encountered. As a rule I do not use new, and it is my
opinion that the new keyword should never be used in this context.

My Questions are:

Shouldn't covariant return be part of csharp, I can think of no reason they
would ever cause a problem?

Is the class below a poor design and is there a better way to achieve the
same effect without using covariant return types, and preferably without
interfaces?

How do you overcome the lack of covariant return types?

Thanks,

Mike

public class Animal
{
public virtual Animal[] Offspring
{
get { return this._offspring }
}
private Animal[] _offspring = null;
}

public class Dog : Animal
{
public override Dog[] Offspring
{
get { return this._offspring }
}
private Dog[] _offspring = null;
}
Nov 17 '05 #1
2 1461
I have been researching this again and it looks like covariant return types
*won't* be in cssharp 2.0 so in the meantime I think this is the best
workaround and would like to hear any comments.

In the example below I have chosen to use the new keyword and even though
some boxing is involved as a rule I will always interact with the base
class, preventing the nasty null error that often occurs with new when you
do not rely on the base class to hold the object reference. I will just
have to live with the performance hit for now. I think this is the best
workaround for the lack of covariant return types, eventhough interfaces can
be used, they introduce other issues. I do not like casting of any kind and
see it as an invitation for runtime errors but this one should be fairly
safe until things get better.

I am also going to research moving to managed C++ at some point if csharp
does not start fully supporting polymorphism.

public class Animal
{
public virtual Animal[] Offspring
{
get { return this._offspring; }
set { this._offspring = value; }
}
private Animal[] _offspring = null;
}

public class Dog : Animal
{
public new Dog[] Offspring
{
get { return base.Offspring as Dog; }
set { base.Offspring = value; }
}
}
Nov 17 '05 #2

"Mike" <mi********@com.nospam> wrote in message
news:eU**************@TK2MSFTNGP12.phx.gbl...
I have been researching this again and it looks like covariant return types
*won't* be in cssharp 2.0 so in the meantime I think this is the best
workaround and would like to hear any comments.

In the example below I have chosen to use the new keyword and even though
some boxing is involved as a rule I will always interact with the base
class, preventing the nasty null error that often occurs with new when you
do not rely on the base class to hold the object reference. I will just
have to live with the performance hit for now. I think this is the best
workaround for the lack of covariant return types, eventhough interfaces
can
be used, they introduce other issues. I do not like casting of any kind
and
see it as an invitation for runtime errors but this one should be fairly
safe until things get better.


Ya, new is the best you are going to get here. Generics might help in 2.0,
but otherwise new is what you are going to have to use.
A generic example:

public abstract class Animal<T> where T : Animal<T>
{
public T[] Offspring
{
get {...}
}
}

public class Dog : Animal<Dog>
{

}
Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Alex Vinokur | last post by:
Hello, Here is some program with virtual constructors. Is there any difference between * clone1() vs. clone2() * create1() vs. create2() ? It seems that it should be.
3
by: Sankar Nemani | last post by:
Hi, Does anyone know what the reason behind not allowing to use "covariant return types when overriding methods in derived classes" in C# is? Also while other OO languages such as Java, don't...
14
by: Stefan Slapeta | last post by:
Hi, this code does not compile in C#: class base_class {} class derived_class : base_class {} class A { public virtual base_class f()
13
by: Stephen Walch | last post by:
Error C2392 is hitting me hard! I have a managed C++ library that implements a bunch of fixed interfaces. For example, one interface is: public abstract interface IDbCommand { public...
16
by: Bob Hairgrove | last post by:
Consider the classic clone() function: class A { public: virtual ~A() {} virtual A* clone() const = 0; }; class B : public A { public:
6
by: miked | last post by:
Why are there still no covariant return types? All searches reveal no workarounds accept for using an interface which is a real pain. I wind up missing this capability almost every time I...
8
by: Alex Vinokur | last post by:
Here is a code from http://www.parashift.com/c++-faq-lite/virtual-functions.html#faq-20.8 -------------------------------------- class Shape { public: virtual ~Shape() { } // A...
3
by: kikazaru | last post by:
Is it possible to return covariant types for virtual methods inherited from a base class using virtual inheritance? I've constructed an example below, which has the following structure: Shape...
9
by: Rahul | last post by:
Hi Everyone, I was trying to implement covariant return types and i get a compilation error, class BB : public AA { }; class A
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.