"Best" way of passing in a big struct to a function?

Jonathan M Davis jmdavisProg at gmx.com
Tue Oct 9 23:54:14 PDT 2012


On Wednesday, October 10, 2012 06:51:50 Val Markovic wrote:
> So if I don't need to support accepting rvalues, is there an
> argument for "in ref" over "const ref"? "in ref" looks superior:
> it's more descriptive and from what the docs say, it gives even
> more guarantees about the behavior of the function.

In general, I'd advise aganist using in. in is an alias for const scope. scope 
is supposed to make it so that no references to that parameter can escape the 
function. This makes it utterly pointless for value types (as structs 
typically are). To make matters worse, it's not even properly implemented for 
anything beyond delegates. So, while something like

int[] foo(scope int[] arr)
{
    return arr;
}

is supposed to be illegal, the compiler currently allows it. So, if you use 
scope (or in) very much, you're going to get all kinds of compilation errors 
once scope has been fixed.

If you want const, use const, but aside from delegates, I wouldn't use scope 
at this point, so I wouldn't use in either. And for many cases, even if scope 
worked correctly, using in instead of const would be pointless, because the 
scope portion wouldn't be applicable and would be ignored.

Personally, I wish that in didn't exist at all. It just causes future bugs at 
this point and adds no value (since you can use const scope if that's what you 
want). But D1 had it (albeit with slightly different semantics), so it's still 
around in D2 for transitional purposes if nothing else.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list