digitalmars.D.learn - Chaining struct method invocations
- Bahman Movaqar (9/9) Sep 07 2015 I need some help understand the behaviour of my code[1].
- mzfhhhh (22/31) Sep 07 2015 struct is a value type,you can convert to ref type by "ref":
- Bahman Movaqar (3/26) Sep 07 2015 Thanks. I was afraid I had to resort to using pointers to
- Namespace (3/12) Sep 07 2015 You should mark your return type with ref. Structs are value
- Bahman Movaqar (5/9) Sep 07 2015 Does this mean that in the following piece of code, what is
- Jacob Carlborg (5/9) Sep 07 2015 Yes. structs have value semantics. If you want reference semantics you
- Bahman Movaqar (3/12) Sep 07 2015 Actually I like the value semantics very much. I think I'm going
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
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.dstruct 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
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);//2Thanks. I was afraid I had to resort to using pointers to achieve this!
Sep 07 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.dYou should mark your return type with ref. Structs are value types and therefore you return only a copy currently.
Sep 07 2015
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
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
On Monday, 7 September 2015 at 14:54:04 UTC, Jacob Carlborg wrote:On 2015-09-07 16:44, Bahman Movaqar wrote:Actually I like the value semantics very much. I think I'm going to stick to `structs` for as much as possible :-)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.
Sep 07 2015