vblang icon indicating copy to clipboard operation
vblang copied to clipboard

[Proposal] ReadOnly As InitOnly Properties

Open VBAndCs opened this issue 4 years ago • 0 comments

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 <InitOnly(Flase)>.

And that is it. No need to confuse users with a new keyword and a new setter! Practically, I implemented my RecGen 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!

VBAndCs avatar Feb 28 '22 20:02 VBAndCs