dotnet / dotnet/vblang

[Proposal] ReadOnly As InitOnly Properties

Open
#614 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
No language data
Stars
328
Forks
71
PR merge metrics
No merged PRs in 30d

Description

I think there is no need at all to add an init-only setter to VB (nor C# in fact!).
The readonly property can do the two jobs it perfectly, as the compiler can use naming convention of the back-filed to init the property as follows:
1. If the filed name is `_{Prpp Name}`, which is the case of auto properties, then the compiler can use the back filed to set the property to value provided in Initialization expression and in the With expression (that I hope to be added to the language).
2. If we need to forbid users from initializing some readonly properties, we can mark it with .

And that is it. No need to confuse users with a new keyword and a new setter!
Practically, I implemented my [RecGen]( https://github.com/VBAndCs/VB-Record-Source-Generator) to treat all ReadOnlly properties as if that are Init-Only, by optionally initializing them throw the Constructor or the With method. I may allow to define positional props as InitOnly instead of readonly if it is desired to omit ReadOnly Properties form the param list of the With method. This shows you that VB is already capable of providing the Init-only functionality without making any change to the property syntax!
In fact I hope Vb some day support my own design of Records, as it is more capable than the C# design, esp when combined with the partial classes to supply more parts such as events.
I may propose this after the next version, when I am thinking of allowing crating `Shared Classes` with the positional syntax!

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.