Re: Passing and returning arguments by ref

2023-03-03 Thread Ali Çehreli via Digitalmars-d-learn
On 3/3/23 12:45, Joe wrote: > I had tried changing B.x1() to: > >`ref X x1() { return [0]; }` > > but the compiler didn't accept it. Yeah, that wouldn't work because the return expression is an X*. Even though 'ref' is implemented as a pointer behind the scenes, that syntax is not legal.

Re: Passing and returning arguments by ref

2023-03-03 Thread Joe via Digitalmars-d-learn
Thanks, Ali. On Friday, 3 March 2023 at 18:09:01 UTC, Ali Çehreli wrote: Think may be due to D not having reference variables. Sometimes one needs to use pointers. Ah! I'm about five chapters away from Pointers ;-). Actually, I had tried changing B.x1() to: `ref X x1() { return [0]; }`

Re: Passing and returning arguments by ref

2023-03-03 Thread Ali Çehreli via Digitalmars-d-learn
On 3/3/23 06:03, Joe wrote: > My understanding was that since A, B and X[] are all reference types, > this ought to work, but obviously something is missing. Think may be due to D not having reference variables. Sometimes one needs to use pointers. I find the following a simpler (and

Passing and returning arguments by ref

2023-03-03 Thread Joe via Digitalmars-d-learn
Let's say we have two classes, A and B. The latter has a dynamic array of X and type X has an add() method that can be used to append elements (of type C, another struct) to X's own dynamic array of C. So it's something like the following: ```d struct C {} struct X { C[] cs; void add(C c)