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

Ola Fosheim Grøstad ola.fosheim.grostad at gmail.com
Tue Jun 7 10:13:35 UTC 2022


On Tuesday, 7 June 2022 at 09:31:20 UTC, zjh wrote:
> For me, `'minimum encapsulation'` is a matter of `principle` 
> and cannot be changed!

I see. If you have lots of principles then D will be a challenge 
for you. It is not a very principled language.

> But `'d'` does not encourage `'minimum encapsulation'`!
> Then you talk with me about `designing language`?

Not exactly sure what you mean. You can do fine without any 
formal encapsulation using conventions. A common convention in 
languages without encapsulation is to prefix private variables 
with underscore, then let the IDE provide warnings when 
"encapsulation" is broken.

This works perfectly well in practice. I've never had a problem 
with it. The reason is that encapsulation is «just a reminder» 
and that works well unless you insist on being stupid. Stronger 
encapsulation is only essential if you redesign larger programs 
or have teams with disloyal programmers who are taking shortcuts.

Encapsulation is not absolute in D, and can never be. You can 
just cast the pointer to a different type and you'1l get full 
access to everything from anywhere.




More information about the Digitalmars-d mailing list