Using closure in function scope to make "real" private class members

Nick Treleaven nick at geany.org
Sat Jun 4 16:53:45 UTC 2022


On Friday, 3 June 2022 at 05:58:06 UTC, Mike Parker wrote:
> The only difference here is that in one case, the function 
> manipulating `_x` is above the brace that terminates `Foo`, and 
> in the other case it's below it. In practical terms, the person 
> editing the module has access to the function in both cases.

I like private in D, but there is a big problem with synchronized 
members:
```d
synchronized class C {
	private int i;
}

public void foo(C c) {
	c.i++;
}
```
```
$ dmd -vasm -c old/syncclass.d
_D9syncclass3fooFCQq1CZv:
0000:   FF 40 08                 inc       dword ptr 8[EAX]
0003:   C3                       ret
```
`foo` just incremented `i` without locking `c`.



More information about the Digitalmars-d mailing list