Required Reading: "How Non-Member Functions Improve Encapsulation"

Kagamin spam at here.lot
Thu Oct 26 10:42:57 UTC 2017


On Wednesday, 25 October 2017 at 22:19:23 UTC, Walter Bright 
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

You mean non-member functions are preferred? I encountered this 
more from performance point: especially in case of small 
structures like Point it would be more beneficial to pass them by 
value than by reference, which can be achieved by extension 
methods, but then you need to import the respective module to 
have those extension methods available. It would work more 
palatable if extension methods from the structure's module were 
available without requiring import. It may rely on static 
declarations inside the struct like C# does it:

struct Size
{
   int width,height;
   alias add=.add; //?
}

Size add(Size s1, Size s2)
{
   return Size(s1.width+s2.width, s1.height+s2.height);
}

or just work without it. Such alias would allow to provide 
extension methods from other modules, though this would mean 
circular dependency between modules.


More information about the Digitalmars-d mailing list