开发者

Dependent types in VB.NET

I was wondering if it is possible to have dependent types in VB.Net 4, or alternatively, if it is possible to construct inherited objects based on the base class' constructor's parameter(s). For example,

Class BaseClass
 开发者_运维百科  Sub New(type as String)
       If type = "One" then
           Me = New Child1  'Assignment to Me is syntax error, but it explains the concept...
       Else
           Me = New OtherChild
       End If
   End Sub
End Class

Class Child1
    Inherits BaseClass
...

Class OtherChild
    Inherits BaseClass
..

..
Sub Main()
   Dim c1 As New BaseClass("One")
   Dim c2 As New BaseClass("Two")
   OverloadedMethod(c1) 'Outputs One
   OverloadedMethod(c2) 'Outputs Two
End Sub

Sub OverloadedMethod(C as Class1)
   Console.Write("One")
End Sub

Sub OverloadedMethod(C as OtherClass)
   Console.Write("Two")
End Sub

EDIT : Explanations about Dependent Types :

Dependent types are types which are constructed based on some parameters(e.g. an scalar value). That is a well-known concept in some (mainly functional) programming languages(e.g. Haskell). For example, in a hypothetical imperative language which supports dependent types, one can write:

Matrix(3,10) A; //A is a 10x10x10 3D Matrix
Matrix(2,3)  B; //B is a 3x3 2D Matrix

And then

A(0,0,0) = 10;
B(0,0) = -2;
B(1,1,0) = 5; // Type Error


EDIT : Considering your comments, I wasn't aware about dependent types. It seems those are not yet implemented in any object oriented programming language. There are research about combining the two of them, bu it seems we're not yet at the step of implementation AFAIK.

At the moment, a constructor cannot construct anything else than the type it is constructing (in your example BaseClass ).

The closest you could get would be with an abstract factory. That would look like this (I kept the string, and made the method static for simplification, but it is usually not part of the pattern, read about it for more details) :

   Class ClassFactory
      Public Shared Function GenerateBaseClassObject(type As String) As BaseClass
         If type = "One" Then
            Return New Child1  
         Else
            Return New OtherChild
         End If
      End Function
   End Class

On the other part of your question, the overloading, that would just make things harder to define your methods outside of your object hierarchy. As you can see, the abstract factory return a BaseClass, and no one knows outside of the factory, and the constructed objects, what is their real type (if we don't consider reflection and casting, that would just be confusing at this point). You should rework your object hierarchy to encapsulate the differences between your two kind of classes.

A complete working refactoring of your code would look like this :

MustInherit Class BaseClass
      Public MustOverride Sub Output()
   End Class

   Class Child1
      Inherits BaseClass

      Public Overrides Sub Output()
         Console.Write("One")
      End Sub
   End Class

   Class OtherChild
      Inherits BaseClass

      Public Overrides Sub Output()
         Console.Write("Two")
      End Sub
   End Class

   Class ClassFactory
      Public Shared Function GenerateBaseClassObject(type As String) As BaseClass
         If type = "One" Then
            Return New Child1
         Else
            Return New OtherChild
         End If
      End Function
   End Class

   Sub Main()
      Dim c1 As BaseClass = ClassFactory.GenerateBaseClassObject("One")
      Dim c2 As BaseClass = ClassFactory.GenerateBaseClassObject("Two")
      c1.Output()
      c2.Output()
      Console.ReadLine()
   End Sub
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜