digitalmars.D.learn - Privatize a few members to allow messing with them #11353
- matheus (10/10) Jun 30 2020 Hi, I was looking the PR in DMD and I found this one:
- Stanislav Blinov (13/15) Jun 30 2020 Before the change, linnum and charnum are public variables, one
- matheus (3/18) Jun 30 2020 Oh I see now and thanks for the information.
- matheus (7/20) Jun 30 2020 By the way:
- Stanislav Blinov (11/15) Jun 30 2020 Almost. Given these definitions:
- matheus (3/21) Jun 30 2020 Alright and thanks again.
Hi, I was looking the PR in DMD and I found this one: https://github.com/dlang/dmd/pull/11353/ One of the changes was: - loc.linnum += incrementLoc; + loc.linnum = loc.linnum + incrementLoc; I usually do the former and I particularly hate the later, so my question is, in this case this was more a style thing than anything else right? Or is there something I'm not able to see? Thanks, Matheus.
Jun 30 2020
On Tuesday, 30 June 2020 at 19:42:57 UTC, matheus wrote:in this case this was more a style thing than anything else right? Or is there something I'm not able to see?Before the change, linnum and charnum are public variables, one can do a += on them. After the change, they become properties accessing, as the PR says, private variables: safe nogc pure property { const uint linnum() { return _linnum; } const uint charnum() { return _charnum; } void linnum(uint rhs) { _linnum = rhs; } void charnum(uint rhs) { _charnum = rhs; } } ...with which the += won't work (at least this variant, as the getter isn't returning ref).
Jun 30 2020
On Tuesday, 30 June 2020 at 19:46:35 UTC, Stanislav Blinov wrote:On Tuesday, 30 June 2020 at 19:42:57 UTC, matheus wrote:Oh I see now and thanks for the information. Matheus.in this case this was more a style thing than anything else right? Or is there something I'm not able to see?Before the change, linnum and charnum are public variables, one can do a += on them. After the change, they become properties accessing, as the PR says, private variables: safe nogc pure property { const uint linnum() { return _linnum; } const uint charnum() { return _charnum; } void linnum(uint rhs) { _linnum = rhs; } void charnum(uint rhs) { _charnum = rhs; } } ...with which the += won't work (at least this variant, as the getter isn't returning ref).
Jun 30 2020
On Tuesday, 30 June 2020 at 19:55:56 UTC, matheus wrote:On Tuesday, 30 June 2020 at 19:46:35 UTC, Stanislav BlinovBy the way: + loc.linnum = loc.linnum + incrementLoc; This works because it was declared: void linnum(uint rhs) { _linnum = rhs; } Right? Matheus.... safe nogc pure property { const uint linnum() { return _linnum; } const uint charnum() { return _charnum; } void linnum(uint rhs) { _linnum = rhs; } void charnum(uint rhs) { _charnum = rhs; } } ...with which the += won't work (at least this variant, as the getter isn't returning ref).Oh I see now and thanks for the information.
Jun 30 2020
On Tuesday, 30 June 2020 at 19:58:05 UTC, matheus wrote:+ loc.linnum = loc.linnum + incrementLoc; This works because it was declared: void linnum(uint rhs) { _linnum = rhs; } Right?Almost. Given these definitions: safe nogc pure property { const uint linnum() { return _linnum; } void linnum(uint rhs) { _linnum = rhs; } } This: loc.linnum = loc.linnum + incrementLoc; is rewritten as: loc.linnum(loc.linnum() + incrementLoc);
Jun 30 2020
On Tuesday, 30 June 2020 at 20:01:43 UTC, Stanislav Blinov wrote:On Tuesday, 30 June 2020 at 19:58:05 UTC, matheus wrote:Alright and thanks again. Matheus.+ loc.linnum = loc.linnum + incrementLoc; This works because it was declared: void linnum(uint rhs) { _linnum = rhs; } Right?Almost. Given these definitions: safe nogc pure property { const uint linnum() { return _linnum; } void linnum(uint rhs) { _linnum = rhs; } } This: loc.linnum = loc.linnum + incrementLoc; is rewritten as: loc.linnum(loc.linnum() + incrementLoc);
Jun 30 2020