Re: Why are there no covariant return types?

From: Stoitcho Goutsev \(100\) [C# MVP] (100_at_100.com)
Date: 07/01/04


Date: Thu, 1 Jul 2004 10:23:32 -0400

Yeah, besides the fact that theoretically you don't know the type of the
object, which Clone methods you call.

And again in theory one should go with the most generic base type and then
do the conversion.

---
Stoitcho Goutsev (100) [C# MVP]
"Jon Skeet [C# MVP]" <skeet@pobox.com> wrote in message
news:MPG.1b4d609128dd0b9698ae12@msnews.microsoft.com...
> Stoitcho Goutsev (100) [C# MVP] <100@100.com> wrote:
> > Could you give us some real life example of where you could use that.
>
> Here's a *really* simple example: ICloneable.
>
> ICloneable defines Clone's return type to be object, when actually you
> almost *always* end up casting the result to the type you know it will
> be - the same type as the instance you're calling it on.
>
> So, if I want to clone an ArrayList, I need:
>
> ArrayList copy = (ArrayList) original.Clone();
>
> If ArrayList could have declared
>
> ArrayList Clone()
> {
> ...
> }
>
> we wouldn't need to do this.
>
> Now in fact, this *is* done all over the framework already, using
> explicit interface implementation - look at the various database
> classes (SqlConnection, SqlCommand etc). It's a nasty hack around the
> lack of covariant return types, and it would have been much nicer if a
> single method could have been declared, instead of one for the generic
> version and one for the concrete subtype.
>
> -- 
> Jon Skeet - <skeet@pobox.com>
> http://www.pobox.com/~skeet
> If replying to the group, please do not mail me too


Relevant Pages

  • Re: ICloneable ... why type it out?
    ... It is far easier, faster, and safer to determine Clone generically ... The issue with ICloneable is that it doesn't specify deep or shallow copy. ... I would certainly, however, recommend an interface or common base class ... instead of just adding Clone methods everywhere. ...
    (microsoft.public.dotnet.general)
  • Re: My Spiderman 3 Reviews
    ... Yeah, I thought that was weird too. ... Peter investigates a house during the 90s clone saga and finds the clone of Gwen there shacked up with a clone of Professor Warren. ... She shows up towards the end of the clone saga and I don't think she's been seen since. ...
    (rec.sport.pro-wrestling)
  • Re: Promise PCI HD in XP Home
    ... > Clone. ... Intel D845 MB. ... This drive shows up in device manager but not in my ... Yeah, find the hard drive manufacturers website, download their disk drive ...
    (alt.comp.hardware.pc-homebuilt)
  • Re: Mutable member variables -- surprising behavior
    ... Eep, yeah, I forgot about that. ... that misinformation. ...
    (comp.lang.ruby)
  • RE: Passing objects by value
    ... yeah, pass in a clone copy. ... "Arghknork" wrote: ... Anyone have a suggestion? ...
    (microsoft.public.dotnet.languages.csharp)