By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,819 Members | 918 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,819 IT Pros & Developers. It's quick & easy.

Generics and Inheritance

P: n/a
Hi,

I would expect that inheritance through the parameters of a generic class
would be fine however I get errors as below in the following code.

Public Class Form1
Sub test()
Dim xxx As Foo(Of DerivedBase)
Dim zzz As Foo(Of Base)

zzz = xxx '*** Foo(DerivedBase) cannot be converted to Foo(Base)
zzz = CType(xxx, Foo(Of Base)) '*** Foo(DerivedBase) cannot be
converted to Foo(Base)

Dim aaa As Bar(Of Base)
zzz = aaa
End Sub
End Class

Public Class Foo(Of XXX As Base)
End Class

Public Class DerivedBase
Inherits Base
End Class

Public Class Base
End Class

Any (clean) workarounds?
Nov 21 '05 #1
Share this Question
Share on Google+
5 Replies

P: n/a
>Any (clean) workarounds?

I'm not sure what kind of workaround you're looking for. But you can
use just a Foo(Of Base) for both Base and DerivedBase instances. Of
course that means you have to cast whenever you need to use
DerivedBase specific functionality.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 21 '05 #2

P: n/a
Yes I can cast however a substantial part of the raison d'etre for generics
is that we remove casting n'est pas? And if I'm going to cast anyway why add
the complexity of generics, it could be implemented as a straight hierarchy
from Base with a Foo interface. That however fixes the implementation and I
lose any chance to reuse the code for other purposes.

I guess my point is that I can see no reason to preclude the syntax and it
does make the implementation cleaner, and it certainly makes for better code
reuse, so why disallow it? It appears the compiler is naively comparing
Foo(Of Base) and Foo(Of DerivedBase) and saying there is no relationship;
whereas there truly is an inheritance relationship just as there is an
inheritance relationship between Base and DerivedBase.

The utility is enhanced if we introduce another class Derived2Base which is
also derived from Base. I can then use a reference to Foo(Of Base) for either
one which is the generics equivalent of being able to use a fruit reference
to access apples or bananas.
"Mattias Sjögren" wrote:
Any (clean) workarounds?


I'm not sure what kind of workaround you're looking for. But you can
use just a Foo(Of Base) for both Base and DerivedBase instances. Of
course that means you have to cast whenever you need to use
DerivedBase specific functionality.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Nov 21 '05 #3

P: n/a
Yes I can cast however a substantial part of the raison d'etre for generics
is that we remove casting n'est pas?
Oui.

I guess my point is that I can see no reason to preclude the syntax and it
does make the implementation cleaner, and it certainly makes for better code
reuse, so why disallow it?
I suggest you read Rick Byers' discussion of the pros and cons of type
parameter variance here

http://blogs.msdn.com/rmbyers/archiv...16/375079.aspx

It appears the compiler is naively comparing
Foo(Of Base) and Foo(Of DerivedBase) and saying there is no relationship;
whereas there truly is an inheritance relationship just as there is an
inheritance relationship between Base and DerivedBase.


No, there isn't. It can be simulated, like they do today with arrays.
But a List(Of DerivedBase) doesn't really inherit from List(Of Base).

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 21 '05 #4

P: n/a
A popular misconception is the only real purpose of generics is to support
collections. And given the implementation in Beta2 I agree that allowing what
I would like to do will cause programs to break in non-trivial ways. A closer
look should be paid to the Java implementation rather than aiming low for a
nice textbite on the box. Put me down for front row tickets to see something
more upmarket in the final release ... or is it that the grass really is
greener where the Sun shines?

"Mattias Sjögren" wrote:
Yes I can cast however a substantial part of the raison d'etre for generics
is that we remove casting n'est pas?


Oui.

I guess my point is that I can see no reason to preclude the syntax and it
does make the implementation cleaner, and it certainly makes for better code
reuse, so why disallow it?


I suggest you read Rick Byers' discussion of the pros and cons of type
parameter variance here

http://blogs.msdn.com/rmbyers/archiv...16/375079.aspx

It appears the compiler is naively comparing
Foo(Of Base) and Foo(Of DerivedBase) and saying there is no relationship;
whereas there truly is an inheritance relationship just as there is an
inheritance relationship between Base and DerivedBase.


No, there isn't. It can be simulated, like they do today with arrays.
But a List(Of DerivedBase) doesn't really inherit from List(Of Base).

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Nov 21 '05 #5

P: n/a
>A closer look should be paid to the Java implementation rather than aiming low for a
nice textbite on the box.
What's so great about it? Last I read, it seemed like a big ugly hack
that didn't provide any real type safety of performance improvement.

Put me down for front row tickets to see something
more upmarket in the final release ...


I wouldn't expect any changes related to this between beta 2 and RTM.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 21 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.