Uranuz:
Same situation happens when I assign reference data to
properties.
Someone has suggested to solve this problem with an attribute,
like owned, that forbids to return mutable reference data owned
by a class/struct instance.
Bye,
bearophuile
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
V Fri, 28 Nov 2014 06:19:37 +
Uranuz via Digitalmars-d-learn digitalmars-d-learn@puremagic.com
napsáno:
In D we a several data types which are passed by reference:
dynamic arrays, associative arrays. And sometimes we need to pass
these reference data to class instance to store it inside.
On Friday, 28 November 2014 at 08:31:26 UTC, bearophile wrote:
Uranuz:
Same situation happens when I assign reference data to
properties.
Someone has suggested to solve this problem with an attribute,
like owned, that forbids to return mutable reference data
owned by a class/struct
Yes. Problem is even if you have property that controls correct
assignment. If you have getter that returns mutable reference
type and you try to access some fields of it or apply index
operator (for arrays or AA) *host* cannot control corectness of
these assignments or cannot react to these
In D we a several data types which are passed by reference:
dynamic arrays, associative arrays. And sometimes we need to pass
these reference data to class instance to store it inside. One of
the principles of object-oriented programming is incapsulation.
So all class data should be only