Extended anonymous types

Topics: C# Language Design, VB Language Design
Aug 16, 2014 at 3:47 PM
Edited Aug 16, 2014 at 3:55 PM
Sometimes, there are needs to create anonymous type, as extension of some object. For now, anonymous types can only contain these objects.
Public Class Point
    Public X As Integer
    Public Y As Integer

    Sub New(X As Integer, Y As Integer)
            Me.X = X
            Me.Y = Y
    End Sub
End Class

Dim p = New Point(10, 20)
Dim exp = New With {.P = p, .Color = "red"}
exp.P.X = 11
Dim exp2 = New With {.EXP = exp, .Z = 30}
exp2.EXP.P.X = 12
But with this container-only possibilities, access to members (X on exp and exp2) is uncomfortable, and performance may be low.


Better idea is to add possibility, where anonymous type 'derives and extend' of some object. Then we can create objects of anonymous types with new way:
Dim p = New Point(10, 20)
Dim exp = p With {.Color = "red"}
exp.X = 11
Dim exp2 = exp With {.Z = 30}
exp2.X = 12
This way, access to members is simplified and performance is better. Variable exp will be created as instance of AnonymousType1, and exp2 as instance of AnonymousType2. These anonymous types should be generated as:
Public Class AnonymousType1
    Dim OrigObj1 As Point
    Public Property X As Integer
        Get
            Return OrigObj1.X
        End Get
        Set(value As Integer)
            OrigObj1.X = value
        End Set
    End Property

    Public Property Y As Integer
        Get
            Return OrigObj1.Y
        End Get
        Set(value As Integer)
            OrigObj1.Y = value
        End Set
    End Property

    Public Property Color As String

    Sub New(OrigObj1 As Point)
        Me.OrigObj1 = OrigObj1
    End Sub
End Class

Public Class AnonymousType2
    Dim OrigObj1 As Point
    Public Property X As Integer
        Get
            Return OrigObj1.X
        End Get
        Set(value As Integer)
            OrigObj1.X = value
        End Set
    End Property

    Public Property Y As Integer
        Get
            Return OrigObj1.Y
        End Get
        Set(value As Integer)
            OrigObj1.Y = value
        End Set
    End Property

    Dim OrigObj2 As AnonymousType1
    Public Property Color As String
        Get
            Return OrigObj2.Color
        End Get
        Set(value As String)
            OrigObj2.Color = value
        End Set
    End Property

    Public Property Z As Integer

    Sub New(OrigObj1 As Point, OrigObj2 As AnonymousType1)
        Me.OrigObj1 = OrigObj1
        Me.OrigObj2 = OrigObj2
    End Sub
End Class
Objects are refreneced than copied, because if we write
exp.Color = "blue"
then exp2 also must have color as blue.
Aug 19, 2014 at 3:14 PM
I think it's important to distinguish that "extending" a type can have two purposes: allow a reference to the latter type to be substituted for the former , or allow the latter type to regard members of an encapsulated instance are its own. I think what you're after is the second, rather than the first; as such, extension really isn't the right paradigm. What's needed instead, I think, is a declarative means via which a type X could say "regard the members of field y as though they are members of this instance", so that given X x, the code X.memberOfY would be synonymous for X.y.memberOfY. I'm not sure what feature would be best, but if e.g. preceding a non-generic field declaration with this was shorthand for "wrap public members of this field's type", such a feature could be applied to anonymous types as to any other types.