dotnet / dotnet/vblang

Nested object and collection initializers

Open
#134 2 comments 5 reactions 0 assignees View on GitHub
Proposal
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.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.