Extending D's support for object-oriented design with private(this)
NotYouAgain
NotYouAgain at gmail.com
Thu Apr 25 09:44:13 UTC 2024
On Thursday, 25 April 2024 at 05:53:18 UTC, Richard (Rikki)
Andrew Cattermole wrote:
> Apart from literature review type content, there isn't much to
> discuss here. There is nothing to tune.
>
> I suggest you start work on a draft DIP and move to development
> unless Walter or Atila have strong opinions against it.
I'd like to see more discussion first.
There are implications for derived classes (that are in the same
module), and nested classes (which of course, are already in the
same module).
The feature would also open up the following 'new idiom' for
creating and 'enforcing' pseudo static classes in D (which I like
and use in OpenD). Some, I believe, may object to allowing such
an idiom in D - but they should put forward their reasoning along
with their objection.
module m;
@safe:
import std;
final class C // final -> class cannot be derived from
{
private(this) this(){} // no instance of this class is
possible
// **not even by other code in the
same module.
private void test() {}; // although non-static members are
allowed, but pointless,
// since they can never be used -
because of private(this)
static:
private(this) string message = "Hello!";
void sayHello() { writeln(message); }
}
unittest
{
C c = new C(); // Error: class `test.C` constructor `this`
is not accessible
writeln(C.message); // Error: property `message` is private
to type `test.C`
// static members are callable on a class even when no
instance of the class exists.
C.sayHello(); // ok
}
More information about the dip.ideas
mailing list