anonymous types and generic collections

Tim Ng has made a post about using a helper function to create a generic list for anonymous types.

The code actually needs a bit of modification, it should really be :

 

Function MakeList(Of T)(ByVal ParamArray items() As T) As List(Of T)
   Dim list As New List(Of T)
   list.AddRange(items)
   Return list
End Function

 

You can then pass to it any number of items for your anonymous type.

 

Still, that being the case, the inference could be made at a more direct level if the language supported an anonymous type placeholder, be it var, ? or some other symbol. That is, it should be possible to write code such as :

 

Dim things As ? = New List(Of ?) {New With {.Description= “meaning of life“, .Age = 42}, _

                                             New With {.Description= “meaning of life sequel“, .Age = 43}}

 

And hence remove the need for yet another helper function.

 

hopefully we can elegantly make the language evolve in such a direction :)       

 

 

 

 


This entry was posted in 2087, 2088, 491. Bookmark the permalink.

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>