Chaining struct method invocations
mzfhhhh via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Mon Sep 7 07:26:56 PDT 2015
On Monday, 7 September 2015 at 14:12:25 UTC, Bahman Movaqar wrote:
> I need some help understand the behaviour of my code[1].
> Specifically I have trouble with `add` method on line 79.
>
> My impression is that since it returns `this`, multiple
> invocations can be chained like `obj.add(X).add(Y).add(Z)`.
> However the test on line 92 fails and if I do a `writeln`, only
> "p1" and "p2" records show up.
>
> What am I missing here? Thanks in advance.
>
> [1]
> https://github.com/bahmanm/d-etudes/blob/master/source/e002/models.d
struct is a value type,you can convert to ref type by "ref":
struct Test
{
int a;
Test add1()
{
a++;
return this;
}
ref Test add2()
{
a++;
return this;
}
}
Test t1;
t1.add1.add1;
writeln(t1.a);//1
Test t2;
t2.add2.add2;
writeln(t2.a);//2
More information about the Digitalmars-d-learn
mailing list