[This post is part of a series, "wish-list for future versions of VB"]

 

IDEA: We should allow "With" as a modifier on expressions. Currently the "With" object-initializer syntax is only allowed on constructors, e.g.

Dim x = New S With {.p = 1, .q = 2}

This is rather limiting. It would be good if we could use it on factory methods, e.g.

Dim y = Factory.CreateThing() With {.p = 1, .q = 2}

The cleanest way to allow this, in the language, is to allow a "With" initializer to come after any expression:

Expression ::=  ...  |  Expression  ObjectMemberInitializer

This would be syntactic sugar for evaluating the expression and then assigning its properties:

Dim $temp = Factory.CreateThing()

$temp.p = 1

$temp.q = 2

Dim y = $temp

 

 

Provisional evaluation from VB team: When you tell the car dealer "I'd like to buy this car but with the yellow paintjob" -- you don't expect him to get out his spraycan and change this car right here; you expect him to go to the back of the store and pick out a similar car but with one difference. Likewise, we think it will be awfully unclear to users that "With" modifies an existing object rather than constructing a new object with some variations. We think that this proposal "digs up more snakes than ladders".