Should out/ref parameters require the caller to specify out/ref like in C#?

Stanislav Blinov via Digitalmars-d digitalmars-d at puremagic.com
Mon May 29 00:46:07 PDT 2017


On Monday, 29 May 2017 at 07:39:40 UTC, Dukc wrote:

> But what would be worth a consideration, is that perhaps one 
> should be allowed to pass rvalues as reference with something 
> like this? According to TDPL, ref arguments do not take rvalues 
> to prevent bugs where you accidently copy something before 
> passing it, and that's a good rationale. But shouldn't it still 
> be allowed explicitly?

Explicitly? It is:

import std.stdio;

struct S
{
     int v;
}

void foo(ref S s)
{
     writeln("took S by ref: ", s.v);
}

void foo(S s)
{
     writeln("took rvalue S...");
     foo(s);      // calls ref overload
}

void main()
{
     S s;
     foo(s);      // calls ref overload
     foo(S.init); // calls rvalue overload
}

And for templates we have auto ref.


More information about the Digitalmars-d mailing list