Generics and Inheritance

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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?
 
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
 
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.
 
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/archive/2005/02/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
 
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?
 
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
 
Back
Top