Preparing for the New DIP Process

FairEnough FairEnough at gmail.com
Fri Jan 26 23:04:34 UTC 2024


On Friday, 26 January 2024 at 22:21:22 UTC, Meta wrote:
>
> This is the only valid reason for introducing class-private 
> that's ever been put forth in this forum. I saw someone else 
> post a similar argument around class invariants awhile back as 
> well and it completely changed my mind on the issue.

This has been argued before, but in a slightly different manner.

Objects have type (i.e. the object is of type class).

As a type, it must be able to present its interface to other code 
in a way that maintains its invariants.

In this view, a class is no different to other types in the 
language, which also maintain their invariants.

The key here, is to think of a class as a type. If you don't 
think that way about classes, then class(private) would seem a 
pretty useless addition to the langauge.

But without a class(private) option, there is simply no way for a 
class 'type' to maintain its invariants in D, when other code is 
in the same module as the class. This includes code for unittests.

The only workaround in D, is to ensure a module (file) contains 
no other code other than that single class.

While module level visibility is very useful, the usefulness of 
class level visibility is not really something that anyone should 
have to justify. A class is a type, and therefore should have the 
ability to maintains its invariants, even from other code in the 
same module.

That, I would argue, is the primary reason for introducing an 
option in D for class(private).

It's not an argument for OOP. It's an arguement to allow a class 
type in D, to be able to present itself 'as type that can 
maintain its own invariants' to other code in the same module. 
The key part there is 'to other code in the same module'. This 
cannot occur without a class level visibility attribute.

The only argument that be put forward against this arguement, is 
that in D, you shouldn't ever need to present a class as 'a type 
that can maintain its own invariants' to other in the same module.

But when a programmer has that need, D cannot provide a solution.

class(private) would immediately solve that problem, for that 
programmer.


More information about the Digitalmars-d-announce mailing list