VB Orcas anonymous syntax feels wrong

Earlier today I noticed a comment from Tim Ng on my early blog about mutable anonymous types. It was one of those comments that just felt wrong…. the more I thought about this the more I realised just how wrong it is.


To allow for mutable and immutable anonymous types, Paul Vick indicated VB will introduce a Key keyword to be used inside the With declaration,, e.g.


Dim customer As New With {Key .Id = 1, .Name = “Fred”}


Now theoretically this will give you a class with this signature:


Class Foo


    Sub New(ByVal id As Int32)
        Me.m_ID = id
    End Sub


    Private m_ID As Int32
    Private m_Name As String


    Public ReadOnly Property ID() As Int32
        Get
            Return m_ID
        End Get
    End Property


    Public Property Name() As String
        Get
            Return m_Name
        End Get
        Set(ByVal value As String)
            m_Name = value
        End Set
    End Property


 End Class


I’ve omitted some of the details such as GetHashCode and Equals etc. Now if I was to try to write the same code with Foo instead of the anonymous type the code would fail.


Dim customer As New Foo With {Key .Id = 1, .Name = “Fred”}


Instead I have to use the only constructor and I can’t set Id, and I can’t set Id to be a key. The only valid syntax is:


Dim customer As New Foo(id) With { .Name = “Fred”}


So in reality we already have ways to work with readonly Properties that are only settable via a constructor. The With {} syntax also disallows setting ReadOnly properties, in fact in the above example intellisense will only display the Name property for Foo, which is all as it should be. But for some reason, one I have yet to fathom, the VB team has decided to allow you to set a ReadOnly property if you preference it with the new Key keyword.


Personally I think the syntax should be the same, be it anonymous types or existing types :


Dim customer As New Foo(id) With { .Name = “Fred”}


and


Dim customer As New(id) With { .Name = “Fred”}


   


  

This entry was posted in Uncategorized. Bookmark the permalink.

2 Responses to VB Orcas anonymous syntax feels wrong

  1. I haven’t been following Orcas a lot lately, but I would have to agree with you Bill.

    This would become confusing for anyone who is swapping back and forth between languages as it’s not standardized constructor.

    I don’t mind that it has a Key keyword, it’s just the constructor call.

    Why couldn’t we have something like
    Dim customer As New(id) With {Key .Id, .Name = “Fred”}

    This makes more sense to me as it displays the class schema as well as the constructor call.

  2. Kurt Frans says:

    Anonymous syntax add’s to a bit of confusion later on when working with lots of code that uses anonymous syntax in several classes throughout a project. you begin to forget what the object type is sometimes, more so when developing several custom object type classes.

    Shouldn’t be able to write the ID in the constructor if it’s ReadOnly, as the property retrieves pre-defined already set internal data. The exception being the Sub New where a value is assigned to the variable.

    Setting the ID via the New Subroutine’s Class Constructor would be a write operation to a private variable, bypassing property method linked to the variable, if the properties value is accessed later on in a ReadOnly manner that makes sense as it is.

    What looks weird is the declaration itself of the new object, I can understand doing it all in 1 line of code though.

    Dim objCustomer As New ObjectType(PropertySetup ObjectType.Property.Name = “Fred”)

    -OR-

    Dim objCustomer As New ObjectType(Properties ObjectType.Property.Name = “Fred”)

    -OR-

    Dim objCustomer As New ObjectType(PropertyDesignations ObjectType.Property.Name = “Fred”)

    -OR-

    Dim objCustomer As New ObjectType(PropertyConfigurations ObjectType.Property.Name = “Fred”)

    -OR-

    Dim objCustomer As New ObjectType.WithDesignatedProperties( ObjectType.Property.Name = “Fred”)

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>