Do non-member functions improve encapsulation in D?

Lars T. Kyllingstad via Digitalmars-d digitalmars-d at puremagic.com
Sun Apr 20 04:12:41 PDT 2014


On Sunday, 20 April 2014 at 11:01:27 UTC, Gary Willoughby wrote:
>
> This is a quote from Walter that sums the reasoning up 
> perfectly:
>
> "A huge reason for them is to head off the temptation to write

What does "them" refer to here?

> ‘kitchen sink’ classes that are filled with every conceivable 
> method. The  desired approach is to have the class implement 
> the bare minimum of functionality, and add other functionality 
> with extension methods (that do not have access to the class’ 
> private state)."

However, in D, all functions defined in the same module as a 
class will have access to the private state of that class, on an 
equal footing with its member methods.  Therefore, the above 
statment doesn't really help in deciding which to use.

> Writing classes like this allows for better encapsulation 
> because only the required behaviour is contained within the 
> class keeping it focused. [...]

I'm also pretty sure Walter has repeatedly stated that the module 
is the unit of encapsulation in D, not the class.


More information about the Digitalmars-d mailing list