Is defining get/set methods for every field overkill?
Dukc
ajieskola at gmail.com
Sat Nov 19 11:26:34 UTC 2022
On Saturday, 19 November 2022 at 03:52:41 UTC, thebluepandabear
wrote:
> Say you want to write 'SET' now whenever someone sets a
> width/height value for the rect (as an example), and 'GET' when
> someone gets the width/height value for the rect, what you
> could do is do this:
>
> ```
> class Rect2D {
> int rectWidth;
> int rectHeight;
>
> int width() {
> writeln("GET");
> return rectWidth;
> }
>
> void width(int rectWidth) {
> writeln("SET");
> this.rectWidth = rectWidth;
> }
>
> int height() {
> writeln("GET");
> return rectHeight;
> }
>
> void height(int rectHeight) {
> writeln("SET");
> this.rectHeight = rectHeight;
> }
> }
> ```
>
> Honestly, it may not be a magic bullet, but still useful.
Yes, this is what I meant. I did use the UFCS name inaccurately:
I believe UFCS means the ability to write `y.x(z)` in place of
`x(y, z)`, and maybe the ability to write `y.x = z` in place of
`y.x(z)`, but it does not mean the ability to omit empty
parenthesis in a function call. The latter is what enables
getters that don't break the API.
More information about the Digitalmars-d-learn
mailing list