Anonymous method remain mutable in VB.

Posted Sat, May 12 2007 10:52 by bill

Paul Vick posted that anonymous types will remain mutable in VB. Generally this looks good, I just hope that it is not a requirement to specify the key. That is if no key is specified, then the hashcode will be generated will be created based on all the fields. One thing that struck me though was in Sree's post about anonymous types being immutable in C# he mentioned threading :
it drastically simplifies the code that uses the object since they can only be assigned values when created and then just used (think threading).

This got me thinking... Given the scope of anonymous types in C# it's not like you could use traditional threading anyway, as they are pretty much limited to within a procedure.. at least as far as the threading you control is concerned. But if you start thinking about iterators, expression trees, delayed evaluation, multi processors, the realization that threading is something that might happen at a different level if everything else falls into place should occur to you. That is, if you set everything up nicely, smart iterators may be able to decide on using multiple threads or not. Ideally this is something we shouldn't have to worry about, but realistically if you want that to work you probably will need to limit your demands. Asking it to do locks and synchronisation on mutable types probably is asking that little bit too much.

The other benefit of having an immutable type is of course the design time experience is explicit that fields are read-only.

VB will of course allow you to create anonymous types that are immutable, mutable or partially mutable. Essentially, you make any property as being readonly by specifying Key before the property name. For example:

Dim projection = From c in Countries Select New With { Key .Country = c.Country, Key .State = c.State }

The short syntax:

Dim projection = From c in Countries Select c.Country, c.State

Will how be a mutable type, the long explicit syntax you can decide which fields are mutable and which are not.

I wonder if it would be more VB'ish to include a modifier for the entire projection. If you start thinking about nested projections etc, it would get difficult to check to see that every member is specified only via the Key.XXX syntax, and hence hard to ensure if the type is immutable. I kind of think it might be nice to be able to write :

Dim projection = From c In Countries Select c.Country, c.State For ReadOnly



Filed under: , ,


# re: Anonymous method remain mutable in VB.

Tuesday, May 29, 2007 11:44 PM by Tim

Hey Bill; actually for queries, we surface the anonymous types as immutable by default. There currently is no way to surface mutable anonymous types in a query projection.

dim q = from c in Countries select c.Country, c.State

surfaces a collection of immutable anonymous types.

Your proposed syntax is interesting though.

# Keyed Anonymous types in VB9

Monday, July 09, 2007 2:05 PM by @ Head

Previously I wrote about my opinion on anonymous types and keys. This post just tells it how it actually