www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Chaining struct method invocations

reply "Bahman Movaqar" <b.movaqar gmail.com> writes:
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
Sep 07 2015
next sibling parent reply "mzfhhhh" <mzfhhhh foxmail.com> writes:
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
Sep 07 2015
parent "Bahman Movaqar" <b.movaqar gmail.com> writes:
On Monday, 7 September 2015 at 14:26:57 UTC, mzfhhhh wrote:
 On Monday, 7 September 2015 at 14:12:25 UTC, Bahman Movaqar 
 wrote:
 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
Thanks. I was afraid I had to resort to using pointers to achieve this!
Sep 07 2015
prev sibling parent reply "Namespace" <rswhite4 gmail.com> writes:
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
You should mark your return type with ref. Structs are value types and therefore you return only a copy currently.
Sep 07 2015
parent reply "Bahman Movaqar" <b.movaqar gmail.com> writes:
On Monday, 7 September 2015 at 14:28:06 UTC, Namespace wrote:
 On Monday, 7 September 2015 at 14:12:25 UTC, Bahman Movaqar 
 wrote:
 Structs are value types and therefore you return only a copy 
 currently.
Does this mean that in the following piece of code, what is passed to `add` is actually a copy of `rec1`? auto rec1 = SalesRecord("p10", 1.0, 10); coll.add(rec1);
Sep 07 2015
parent reply Jacob Carlborg <doob me.com> writes:
On 2015-09-07 16:44, Bahman Movaqar wrote:

 Does this mean that in the following piece of code, what is passed to
 `add` is actually a copy of `rec1`?

     auto rec1 = SalesRecord("p10", 1.0, 10);
     coll.add(rec1);
Yes. structs have value semantics. If you want reference semantics you might want to use a class instead. -- /Jacob Carlborg
Sep 07 2015
parent "Bahman Movaqar" <b.movaqar gmail.com> writes:
On Monday, 7 September 2015 at 14:54:04 UTC, Jacob Carlborg wrote:
 On 2015-09-07 16:44, Bahman Movaqar wrote:

 Does this mean that in the following piece of code, what is 
 passed to
 `add` is actually a copy of `rec1`?

     auto rec1 = SalesRecord("p10", 1.0, 10);
     coll.add(rec1);
Yes. structs have value semantics. If you want reference semantics you might want to use a class instead.
Actually I like the value semantics very much. I think I'm going to stick to `structs` for as much as possible :-)
Sep 07 2015