Using memberspaces for a property-like syntax and more
TommiT
tommitissari at hotmail.com
Sat Feb 2 15:23:36 PST 2013
On Saturday, 2 February 2013 at 22:18:52 UTC, Jonathan M Davis
wrote:
> [..] All these other suggestions are
> just complicating things to no real benefit.
You say "there's no real benefit". I say "there's the benefit of
being able to add another layer of encapsulation when it's
needed". Here's what I mean by that:
The lack of encapsulation with @property attribute:
struct T
{
private int _value;
void add_twice(int v)
{
_value += 2 * v;
}
}
struct S
{
private T _t;
private int _sum_of_squares; // Can't update this
@property ref T prop()
{
return _t;
}
}
void main()
{
S s;
s.prop.add_twice(); // *Not* incrementing s._sum_of_squares
}
...Whereas with memberspaces we can add that extra layer of
encapsulation:
struct S
{
private T _t;
private int _sum_of_squares;
memberspace prop
{
ref const(T) opCall() const
{
return _t;
}
void add_twice(int v)
{
_sum_of_squares += (2 * v)^2; // Yippee!
return _t.add_twice(v);
}
}
}
More information about the Digitalmars-d
mailing list