no reflexive generic type?

  • Thread starter Thread starter PJ6
  • Start date Start date
P

PJ6

I'd like to be able to declare a function where it always returns the type
of the inheritor. To show this in as simple an example that I can think of,
where MyClassType is a reflexive generic type -

Public Class BaseClass
Public Function TestFunction() as MyClassType
Return Me
End Function
End Class

Public Class InheritedClass
Inherits BaseClass
End Class

I would like the the compiler to interpret the return type of
InheritedClass.TestFunction as InheritedClass, so I would never have to cast
from the base class, or write a type-specific function for each inheritor.

I admit this is a bit obscure and not exactly necessary - but I was just
wondering, is this in the works?

Paul
 
I'd like to be able to declare a function where it always returns the type
of the inheritor. To show this in as simple an example that I can think of,
where MyClassType is a reflexive generic type -

Public Class BaseClass
Public Function TestFunction() as MyClassType
Return Me
End Function
End Class

Public Class InheritedClass
Inherits BaseClass
End Class

I would like the the compiler to interpret the return type of
InheritedClass.TestFunction as InheritedClass, so I would never have to cast
from the base class, or write a type-specific function for each inheritor.

I admit this is a bit obscure and not exactly necessary - but I was just
wondering, is this in the works?

Paul

What you are referring to is called covariant return types. It is not
explicitly supported in the CLR, but you can simulate it using
interfaces. The basic pattern is as follows.

Public Interface IMyInterface
Function MyMethod() As BaseClass
End Interface

Public Class MyClass
Implements IMyInterface

Private Function IMyInterface_MyMethod() As BaseClass _
Implements IMyInterface.MyMethod
End Function

Public Function MyMethod() As SubClass
End Function
End Class

Brian
 
PJ6,
In addition to the other comments:

A trick that CSLA.NET uses is:
Public Class BaseClass(Of T As BaseClass)
Public Function TestFunction() as T
Return Me
End Function
End Class
Public Class InheritedClass
Inherits BaseClass(Of InheritedClass)
End Class

This assumes you are using VS 2005 (.NET 2.0) and comfortable with Generics.

Normally however I do as Brian showed...
 
PJ6,
In addition to the other comments:

A trick that CSLA.NET uses is:


This assumes you are using VS 2005 (.NET 2.0) and comfortable with Generics.

Normally however I do as Brian showed...

--

That's actually a pretty cool pattern. I might prefer that when using
2.0. I believe I saw this (or some variant) used in Java's JScience
library, but it's been awhile so I could be wrong.
 
Jay B. Harlow said:
A trick that CSLA.NET uses is:

This is where we get into why I think there is a need for the reflexive
generic type. At least in my version (2.0) this code does not compile
because the (Of T As BaseClass) declaration fails recursively. So you could
do this -

Public Class BaseClass(Of T)
Public Function TestFunction() As T
Return DirectCast(DirectCast(Me, Object), T)
End Function
End Class

Public Class InheritedClass
Inherits BaseClass(Of InheritedClass)
End Class

.... which introduces a way for the code to fail on type safety at runtime
and not compile time, if the developer forgets to update or otherwise
mismatches (Of InheritedClass) in any of the BaseClass inheritors.

Paul
 
PJ6,
Oops, you are correct, the constraint is invalid...

Two more reasons I prefer Brian's method ;-)


--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
 
Back
Top