Extending D's support for object-oriented design with private(this)
Quirin Schroll
qs.il.paperinik at gmail.com
Thu May 23 17:07:17 UTC 2024
On Saturday, 27 April 2024 at 05:44:15 UTC, NotYouAgain wrote:
> Why you so determined to ensure a programmer never has the
> option of explicately declaring that boundary to other code in
> the class. I just don't get it. I can only presume you don't to
> oop, or you put every class in its own module, and every
> unittest for that class its own module as well.
D simply isn’t an OOP-first language, but a language with some
OOP support, actually quite similar to C++. And by your
arguments, I would come to believe, even Java isn’t committed to
OOP because Java’s default visibility is package-wide, not
private to the class.
D’s default visibility is `public` except for imports (where it
would make zero sense). Why? Because D is first and foremost a
pragmatic language.
I have worked with quite a number of languages and most languages
have no notion of module. (Visual Basic has, but it means little,
really.) D’s modules are intended to be narrow, i.e. public stuff
you put in a module is (in your case) your class plus some free
functions that operate on it. Private stuff in a module would be
some utility stuff (functions, types, whatever). Because unlike
Java, not everything D must be in a class, i.e. D has
module-level functions, one class = one file makes no sense. So D
uses modules.
Other languages like C++ or C# have no modules that could be
target of a visibility attribute. Setting aside `protected`, the
next higher up visibility in Java and C# is package-wide or
assembly-wide visibility. In C++, there’s just `public`. C# has a
`file` visibility. Why? Because C# has partial types and there it
makes sense for something to be a file-local utility that the
programmer doesn’t need elsewhere. (It’s usually used for
generated code, though.)
In C++, essentially, everything is in one file, if you ignore
C++20 modules.
All in all, visibility is very much different in different
languages. D’s module-wide private isn’t so weird after all. The
closest thing is C#’s `file` visibility, which serves a
completely different purpose. In C#, you can’t even say whether
`file` or `private` is more narrow.
All in all, you’re mistaken in believing that all languages do
visibility essentially the same and D is the odd one out.
More information about the dip.ideas
mailing list