Passing reference data to class and incapsulation

mark_mcs via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Nov 28 00:47:18 PST 2014


> Same situation happens when I assign reference data to 
> properties. I can check or do something with data at the moment 
> of assignment, but I can't control that someone will modify 
> using initial reference from outside. So do you copy reference 
> data in constructors or properties? Should it be? Or call site 
> should be responsible for not escaping reference somewhere 
> outside and not modifying these data badly?

I'd probably do one of three things:
     *  Clone a private copy;
     *  Pass the parameter as an immutable ref;
     *  Change the method signature to take a std.typecons.Unique!T
to express the fact that I want transfer of ownership.

Try to encode as much information as you can into the method
signature to describe your intentions.


More information about the Digitalmars-d-learn mailing list