Wed Oct 17 - Avoiding Code Smells by Walter Bright

Sebastien Alaiwan ace17 at free.fr
Fri Nov 9 08:17:03 UTC 2018


On Thursday, 8 November 2018 at 23:50:18 UTC, TheFireFighter 
wrote:
> i.e. better encapsulation really is a good thing (although for  
> many, it a lesson that needs to be learned).

Public/private/protected are hacks anyway - and many 
object-oriented languages don't have it. They only provide 
extremely limited encapsulation ; the client still sees the 
non-public part, and can depend on it in unexpected ways:

// my_module.d
struct MyStruct
{
private:
   char[1024] data;
}

class MyClass
{
protected:
   abstract void f();
};

// main.d
import my_module;
import std.traits;
import std.stdio;

int main()
{
   // depends on the list of private
   writefln("MyStruct.sizeof: %s", MyStruct.sizeof); members

   // depends on wether 'f' is declared abstract or not.
   writefln("isAbstractClass!MyClass: %s", 
isAbstractClass!MyClass);

   return 0;
}

If you want perfect encapsulation, use interfaces (as already 
said in this thread), or PIMPL.



More information about the Digitalmars-d-announce mailing list