' Collection of code snippets by Arne Vajhøj 
' (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
﻿Imports System

Namespace CoGen
    Public Class Program
        ' **** demo classes and interfaces ****
        Public Class P
            Public Sub MP()
            End Sub
        End Class
        Public Class C
            Inherits P
            Public Sub MC()
            End Sub
        End Class
        Public Interface IReadonlyContainer(Of  Out T)
            'void Put(T o); // in arg does not match "out"
            Function [Get]() As T
        End Interface
        Public Interface IWriteonlyContainer(Of  In T)
            Sub Put(o As T)
            'T Get(); // return value does not match "in"
        End Interface
        Public Class Container(Of T)
            Implements IReadonlyContainer(Of T)
            Implements IWriteonlyContainer(Of T)
            Private o As T
            Public Sub Put(o As T) Implements Program.IWriteonlyContainer(Of T).Put
                Me.o = o
            End Sub
            Public Function [Get]() As T Implements Program.IReadonlyContainer(Of T).[Get]
                Return o
            End Function
        End Class
        ' **** covariant methods ****
        Public Shared Sub TestP_ReadOnly(co As IReadonlyContainer(Of P))
            'co.Put(new P()); // problem if C or a subclass of C
            'co.Put(new C()); // problem if a subclass of C
            Dim pcopy As P = co.[Get]()
            pcopy.MP()
            'pcopy.MC();
            'C ccopy = co.Get(); // problem if P
            'ccopy.MP();
            'ccopy.MC();
        End Sub
        Public Shared Sub TestC_ReadOnly(co As IReadonlyContainer(Of C))
            'co.Put(new P()); // problem if C or a subclass of C
            'co.Put(new C()); // problem if a subclass of C
            Dim pcopy As P = co.[Get]()
            pcopy.MP()
            'pcopy.MC();
            Dim ccopy As C = co.[Get]()
            ccopy.MP()
            ccopy.MC()
        End Sub
        ' **** contravariant methods ****
        Public Shared Sub TestP_WriteOnly(co As IWriteonlyContainer(Of P))
            co.Put(New P())
            co.Put(New C())
            'P pcopy = co.Get(); // problem if superclass of P
            'pcopy.MP();
            'pcopy.MC();
            'C ccopy = co.Get(); // problem if P or a superclass of P
            'ccopy.MP();
            'ccopy.MC();
        End Sub
        Public Shared Sub TestC_WriteOnly(co As IWriteonlyContainer(Of C))
            'co.Put(new P()); // problem if C
            co.Put(New C())
            'P pcopy = co.Get(); // problem if superclass of P
            'pcopy.MP();
            'pcopy.MC();
            'C ccopy = co.Get(); // problem if P or a superclass of P
            'ccopy.MP();
            'ccopy.MC();
        End Sub
        ' *****
        Public Shared Sub Main(args As String())
            TestP_ReadOnly(New Container(Of P)())
            TestP_ReadOnly(New Container(Of C)())
            'TestC_ReadOnly(new Container<P>()); // P does not match "out C"
            TestC_ReadOnly(New Container(Of C)())
            TestP_WriteOnly(New Container(Of P)())
            'TestP_WriteOnly(new Container<C>()); // C does not match "in P"
            TestC_WriteOnly(New Container(Of P)())
            TestC_WriteOnly(New Container(Of C)())
        End Sub
    End Class
End Namespace
