Thread overview
Privatize a few members to allow messing with them #11353
Jun 30, 2020
matheus
Jun 30, 2020
Stanislav Blinov
Jun 30, 2020
matheus
Jun 30, 2020
matheus
Jun 30, 2020
Stanislav Blinov
Jun 30, 2020
matheus
June 30, 2020
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.
June 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).
June 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:
>
>> 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).

Oh I see now and thanks for the information.

Matheus.
June 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 Blinov
>> ...
>>     @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.

By the way:

+            loc.linnum = loc.linnum + incrementLoc;

This works because it was declared:

void linnum(uint rhs) { _linnum = rhs; }

Right?

Matheus.
June 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);
June 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:
>
>> +            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);

Alright and thanks again.

Matheus.