Should 'in' Imply 'ref' as Well for Value Types?

Vijay Nayar madric at gmail.com
Fri May 4 09:15:57 UTC 2018


While working on a library built for high efficiency, avoiding 
unnecessary copies of structs became an issue.  I had assumed 
that `in` was doing this, but a bit of experimentation revealed 
that it does not.  However, `ref in` works great.

My question is, should `in` by default also imply `ref` for value 
types like structs?  Is there a reason not to do this?

This is the test program I used for reference:

```
import std.stdio;

struct Bob {
     int a;
     this(this) {
       writeln("<Bob copied>");
     }
}

void main()
{
     Bob b = Bob(3);
     writeln("                &b = ", &b);
     void showAddrIn(in Bob b) {
         writeln("(showAddrIn)    &b = ", &b);
     }
     showAddrIn(b);
     void showAddrRefIn(ref in Bob b) {
         writeln("(showAddrRefIn) &b = ", &b);
     }
     showAddrRefIn(b);
}
```

The output is as follows:

```
                 &b = 7FFD9F526AD0
<Bob copied>
(showAddrIn)    &b = 7FFD9F526AB0
(showAddrRefIn) &b = 7FFD9F526AD0
```


More information about the Digitalmars-d mailing list