Why people dislike global variables so much while I find them so convenient?

rempas rempas at tutanota.com
Thu Jan 27 08:24:21 UTC 2022


On Wednesday, 26 January 2022 at 16:04:36 UTC, Steven 
Schveighoffer wrote:
> A property is a functional setter/getter (or only one of those) 
> that prevents incorrect usage.
>
> For example, if you have a global int, and it really should 
> only be in the range of 1 to 1000, you can guard the setting of 
> the int to make sure that value was correct.
>
> Or if you have a class reference stored globally, you may want 
> to prevent someone from reassigning the class reference to 
> another object. Then you can restrict access to getting only, 
> and not allow setting.
>
> You can implement a property in D at global level, e.g.:
>
> ```d
> private Resource _resource;
>
> public Resource resource() { return _resource; } // only can 
> access via this property
> ```
>
> -Steve

Oh so it was a class property like I thought! Thanks for the 
explanation!


More information about the Digitalmars-d mailing list