OT: Programming Expansibility

Jeffery via Digitalmars-d digitalmars-d at puremagic.com
Thu Oct 22 22:17:46 PDT 2015


On Thursday, 22 October 2015 at 18:36:46 UTC, Idan Arye wrote:
> On Thursday, 22 October 2015 at 17:01:55 UTC, Jeffery wrote:
>> ... (I don't see how wrapping breaks encapsulation, in fact, 
>> it adds another layer of encapsulation, which isn't breaking 
>> it, is it?)
>>
>>
>> The work argument was my whole point though. If the compiler 
>> internally wrapped all unwrapped members(easy to do as it is 
>> just a simple forwarding proxy) and D itself can do this with 
>> just a few lines of code and opDispatch, there is little work 
>> the programmer actually has to do.
>>
>> The issue about private wrapping is moot as I mentioned about 
>> all members being public in the examples. I should have stated 
>> that in general. Obviously wrapping private members wouldn't 
>> render the "private" meaningless.
>
> I did not argue that it's a lot of work - I argue that getting 
> the compiler to do that for you is a bad idea. The point of 
> encapsulation is that you make a concise choice of which 
> members to wrap and how to wrap them. The compiler can't make 
> these design choices, so if the wrapping is done automatically 
> by the compiler it'll simply wrap everything in a 
> straightforward manner, and you miss the whole point of the 
> encapsulation.

Oh, Well, I don't think it is arbitrary. If you expose a public 
member, then you are not encapsulating it and exposing it to the 
public.

The compiler does know this. Hence adding any wrapper does not 
change the encapsulation.

If x is a public field of a class, then how can wrapping it hurt 
anything. If you expose the internal public members of x(if any), 
then this might create less encapsulation than you want. If you 
want to hide some public members of x then surely it is not hard 
to inform the compiler to do so? Explicitly marking the public 
members of x you want to hide should be easy enough:

public class X
{
    public void foo();
}
public class A
{
    public X x;
    private x.foo;  // Marks x as private so compiler does not 
implement wrapper for x.foo

}

(of course, better notations would exist).

Essentially the logic is "wrap by default". Since most of the 
time the programmer is creating wrappers, this seems it would 
save a lot of work?




More information about the Digitalmars-d mailing list