Thread overview | |||||||||
---|---|---|---|---|---|---|---|---|---|
|
September 07, 2015 Chaining struct method invocations | ||||
---|---|---|---|---|
| ||||
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 |
September 07, 2015 Re: Chaining struct method invocations | ||||
---|---|---|---|---|
| ||||
Posted in reply to Bahman Movaqar | 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
|
September 07, 2015 Re: Chaining struct method invocations | ||||
---|---|---|---|---|
| ||||
Posted in reply to Bahman Movaqar | 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.
|
September 07, 2015 Re: Chaining struct method invocations | ||||
---|---|---|---|---|
| ||||
Posted in reply to mzfhhhh | 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!
|
September 07, 2015 Re: Chaining struct method invocations | ||||
---|---|---|---|---|
| ||||
Posted in reply to Namespace | 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);
|
September 07, 2015 Re: Chaining struct method invocations | ||||
---|---|---|---|---|
| ||||
Posted in reply to Bahman Movaqar | 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 |
September 07, 2015 Re: Chaining struct method invocations | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jacob Carlborg | 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 :-)
|
Copyright © 1999-2021 by the D Language Foundation