Passing and returning arguments by ref

Ali Çehreli acehreli at yahoo.com
Sat Mar 4 03:13:54 UTC 2023


On 3/3/23 12:45, Joe wrote:

 > I had tried changing B.x1() to:
 >
 >    `ref X x1() { return &xs[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.

 > It's a bit weird that by taking the address of calling B.x1() and thus
 > getting an X*

Agreed but that's how it is. Taking the address of a reference produces 
an X*.

 > I had to *dereference* that to pass it to the helper
 > function of A.mfd() which actually takes a `ref C` argument.

Yep, all of that makes sense. :)

Ali



More information about the Digitalmars-d-learn mailing list