Nested object and collection initializers
- Dominant language
- No language data
- Stars
- 328
- Forks
- 71
- PR merge metrics
- No merged PRs in 30d
Description
Today in VB you can initialize an object during construction with the `With` and `From` keywords but this only works for initializing the object itself, not any properties of its members. This is a problem when a member is a read-only property which returns an instanced mutable object because you can't simply assign a new object or collection to the property; the API contract requires you to manipulate the object it gives you.
I propose allowing nested object and collection initializers to solve this problem. This will allow the instantiation and initialization of deeper object graphs in an expression context.
``` VB.NET
Dim order = New Order {
.Id = Guid.NewGuid(),
.AddressInfo With {
.Line1 = "233 S Wacker Dr",
.City = "Chicago"
}
.Items From {
New Item With { .ProductId = 1, .Quantity = 1 } }
}
}
```
**Q: Should we support dotted names on the left instead of (or in addition to) the nested With initializer?**
``` VB.NET
Dim order = New Order {
.Id = Guid.NewGuid(),
.AddressInfo.Line1 = "233 S Wacker Dr",
.AddressInfo.City = "Chicago"
}
```
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.