Extending D's support for object-oriented design with private(this)

NotYouAgain NotYouAgain at gmail.com
Sun Apr 28 08:26:03 UTC 2024


On Saturday, 27 April 2024 at 22:59:29 UTC, Steven Schveighoffer 
wrote:
> On Friday, 26 April 2024 at 23:42:48 UTC, NotYouAgain wrote:
>
>> I don't agree with any of those propositions.
>
> OK, and that is understandable. What I'm trying to convey is 
> that *this is a design decision*. You may not agree with it, 
> but it is not something that I think is "fundamental" to OO 
> programming.
>
> I don't need to continue this, I've said my piece, and I'm not 
> interested in further discussion on this. If you end up with a 
> more formal proposal I likely will argue against it, but no 
> hard feelings, I fully understand that there are certain points 
> of view that I don't agree with but that reasonable people will 
> think is correct or better.
>
> We can agree to disagree, and still be friends ;)
>
> -Steve

We can be friends, sure, but this unittest below is certainly not 
my friend ;-)

Yes, I know the solution you will come up with. Put these two 
classes in separate modules,
and make sure the unittest is not in the same module as Point;

D really is module-oriented-programming, not 
object-oriented-programming.

Its claim to support oop is rather dubious. One would need 
private(this) and protected(this), and then D could claim to 
support object-oriented-programming.

module m;
unittest
{
    import std;

    ColoredPoint c = new ColoredPoint(2, 4);

    writefln("%s : %s", c.x, c.y); // 2 : 4

    c.reset(); // actually this shouldn't even be allowed. It is 
not in the intent of the design.
    c.clear(); // only the clear() method should be using reset()

    writefln("%s : %s", c.x, c.y); // 0 : 0

}

class Point
{
   private int x, y;

   this(int x, int y)
   {
       this.x = x;
       this.y = y;
   };

   // the intent here, is that reset() be only callable from 
ColoredPoint.
   // but because protected is also public inside a module, there 
is
   // no way in D to 'declare' that intent.
   protected void reset() { this.x = 0; this.y = 0; }
}

class ColoredPoint : Point
{
     this(int x, int y)
     {
         super(x, y);
     };

     void clear() { reset(); }
}



More information about the dip.ideas mailing list