Required Reading: "How Non-Member Functions Improve Encapsulation"
H. S. Teoh
hsteoh at quickfur.ath.cx
Mon Oct 30 17:40:56 UTC 2017
On Wed, Oct 25, 2017 at 03:19:23PM -0700, Walter Bright via Digitalmars-d wrote:
> for core D devs.
>
> "How Non-Member Functions Improve Encapsulation" by Scott Meyers
>
> http://www.drdobbs.com/cpp/how-non-member-functions-improve-encapsu/184401197
>
> Note that I'm as guilty as anyone for not understanding or following
> these guidelines. I expect we can do much better.
Page 2 of this article is essentially another reason why UFCS in D
totally rawkz. In D, we can take Scott's advice *without* suffering
from syntactic inconsistency between member and non-member functions:
// C++:
class C {
public:
int method();
private: ...
};
int anotherMethod(C &c, ...);
C c;
c.method();
anotherMethod(c); // <-- syntactic inconsistency
// D:
class C {
public int method();
private: ...
}
int anotherMethod(C c, ...);
C c;
c.method();
c.anotherMethod(); // <-- Uniform syntax FTW!
Arguably, this means in D encapsulation is even better than in C++: the
user doesn't even have to care whether a function is a member or not.
The same syntax does the Right Thing(tm).
Furthermore, if the class implementation changes in a drastic way that
makes it possible to make a current member function a non-member, we can
do it in D without needing to touch any client code at all!
T
--
Кто везде - тот нигде.
More information about the Digitalmars-d
mailing list