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

Steven Schveighoffer schveiguy at yahoo.com
Thu Oct 26 12:19:33 UTC 2017


On 10/25/17 6:19 PM, 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 
> 
> 
> Note that I'm as guilty as anyone for not understanding or following 
> these guidelines. I expect we can do much better.

I'm pretty sure I read that before.

However, D's lookup rules fail miserably when it comes to templates:

mod1.d:

auto callFoo(T)(T t)
{
   return t.foo;
}

mod2.d:

struct S
{
    int x;
}

int foo(S s) { return s.x * 5; }

void main()
{
    auto s = S(1);
    assert(s.foo == 5);
    assert(s.callFoo == 5); // can't compile
}

Would be nice to have a way around this. Not sure what it would look like.

Also in D, it's harder to make this help for encapsulation since the 
non-member functions would have to be in another module.

-Steve


More information about the Digitalmars-d mailing list