473,735 Members | 2,090 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Covariant return types

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 inherit a
class. The best workaround and one that I use is the new keyword and cast
the base class reference, but in my opinion this is a bad practice leading to
error prone code.

Can someone from MS chime in on this and provide some feedback?

Thanks,

Mike
May 24 '06 #1
6 2554

"miked" <mi***@discussi ons.microsoft.c om> wrote in message
news:57******** *************** ***********@mic rosoft.com...
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 inherit
a
class. The best workaround and one that I use is the new keyword and cast
the base class reference, but in my opinion this is a bad practice leading
to
error prone code.

Can someone from MS chime in on this and provide some feedback?

I'm not MS, far from it, but here's a link to an MS webpage with a
semi-official position on covariant return types:

http://lab.msdn.microsoft.com/produc...5-71ecd52d1840

"Postponed"

Andrew
May 24 '06 #2
And here's another nugget, from
http://msdn.microsoft.com/chats/tran...o_092304.aspx:

Host: peterhal (Microsoft)
Q: Why doesn't C# allow Covariant Return Types?
(http://weblogs.asp.net/okloeten/arch...24/119580.aspx)
A: Overrides in C# are just reimplementatio n of base members. They do not
participate in member lookup or overload resolution. As such, variance in
the override implementation would not be available from the callsite on the
derived object without significant (breaking) changes to our member lookup
rules.
Not sure I quite understand what they are saying - does the override method
somehow poke itself where the base method was? Time to look at some MSIL.

Andrew
May 24 '06 #3
Andrew,

Thanks for the links and info...

I have read before that covariant return types are not necessary which I
guess is true but to get around the lack of covariant return types you have
3 options assuming class A : B

1) Use and interface
- Klunky / Bloated / Alot of work
+ None

2) public new B AProperty { get { (B)base.AProper ty;} }
- Uneccesary Casting / Runtime Errors
+ Best solution if one day we get covariant return types or explicit
class member declaration

3) public override A AProperty { get { return this.BProperty; } }
public B BProperty { get { return this._bProperty } }
private B _b;
- What a mess this is and will require a lot of rewriting if covariant
return types are one day allowed.

Maybe MS could at least provide explicit member declarations for classes as
shown below, still messy, works like an interface without having to code an
interface just to make up for the lack of covariant return types.

public override A A.AProperty { get { return base.AProperty; } }
public B AProperty { get { return base.AProperty; } }

Or... Maybe I simply don't understand OOP programming and inheritance if so
I wish someone would set me straight as I literally hit the covariant return
type wall almost every day.

Thanks,

Mike

May 24 '06 #4
I agree that covariant return types are gold. I use them in C++, my main
language, all the time. I remember the sigh of relief when VC++ finally
supported it.

I'm hoping that the C# guys will find a way to make covariance work and
until then I think your new/cast method is the best workaround.

Andrew
May 24 '06 #5
And it's a pity no one told the folks who wrote the WinForms DataGrid
which *requires* covariant returns for one feature. (with an empty
collection, it will base the column headings by getting the data type
of the return value of the Item property(Indexe r) of it's DataSource.
However, the data source at that point is an IList, and IList defines
the return value of Item a Object.)

May 24 '06 #6


miked wrote:
I have read before that covariant return types are not necessary which I
guess is true but to get around the lack of covariant return types you have
Lost of things are in C# that are not "necessary" : foreach, lock, using,
yield-return, ... and a lot more are coming in C#3.

They're dead-handy though, and so is co-variant return-types.
3 options assuming class A : B

1) Use and interface
- Klunky / Bloated / Alot of work
And, it doesn't solve any typing issues, it simply moves them.
+ None

2) public new B AProperty { get { (B)base.AProper ty;} }
- Uneccesary Casting / Runtime Errors
a. The cast is not necessary for co-variant returns, it's necessary
because you have (type-wise) unexpressed knowledge that in this instance
base.AProperty should be not only A, but B.

b. A runtime-error would show that your unexpressed knowledge is false
+ Best solution if one day we get covariant return types or explicit
class member declaration

3) public override A AProperty { get { return this.BProperty; } }
public B BProperty { get { return this._bProperty } }
private B _b;
- What a mess this is and will require a lot of rewriting if covariant
return types are one day allowed.
Not to mention that A may still have storage reserved for a now unused
AProperty, all in all -- not really text-book inheritance.
Maybe MS could at least provide explicit member declarations for classes as
shown below, still messy, works like an interface without having to code an
interface just to make up for the lack of covariant return types.

public override A A.AProperty { get { return base.AProperty; } }
public B AProperty { get { return base.AProperty; } }
I'm not sure I like that very much. I think i prefer that Properties of
objects stay the same no matter what static-type I know the origin
object as, so

B b = /* some B */
A a = b;
a.AProperty == b.AProperty;

but not nessesarily:

typeof(a.APrope rty) == typeof(b.APrope rty)
Or... Maybe I simply don't understand OOP programming and inheritance if so
I wish someone would set me straight as I literally hit the covariant return
type wall almost every day.


I use the:

T new P { get { return (T) base.P; } }

in-between, and it works very well for me.

Be aware that this pattern requires a corresponding "set" in the
base-class is counter-variant, and should be virtual - if it exists. The
object can then verify that it's assertion on base.P.GetType( ) isn't
violated.

One of the reasons that co/counter-variant support isn't included is
that in order to utilize it, without making mistakes, you need to think
quite a bit.

--
Helge
May 25 '06 #7

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

Similar topics

7
2148
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
1545
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 allow todo the same, why does C++ allow it and then the C# and VB.NET don't allow? I just want to understand the logical reasoning behind this decision if there is one.
14
2005
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
4269
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 abstract new System.Data.IDbConnection Connection }
2
1482
by: Mike | last post by:
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...
16
3200
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:
8
2175
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 virtual destructor virtual void draw() = 0; // A pure virtual function virtual void move() = 0; ...
3
4550
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 = base class Triangle, Square = classes derived from Shape Prism = class derived from Shape TriangularPrism, SquarePrism = classes derived from Triangle and Prism, or Square and Prism respectively
9
1877
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
8959
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8784
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9248
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9199
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8199
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6747
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6049
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4821
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3270
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.