ref and out parameters on call site

WebFreak001 d.forum at webfreak.org
Mon Mar 30 11:01:56 UTC 2020


On Wednesday, 18 March 2020 at 17:46:22 UTC, Paul Backus wrote:
> On Wednesday, 18 March 2020 at 17:23:26 UTC, WebFreak001 wrote:
>> Any ideas? Counter arguments why this shouldn't be implemented 
>> other than old code breakage?
>
> How would `auto ref` and core.lifetime.forward work with this?

forward kind of seems like magic with alias right now, so it will 
probably either break permanently or need to be reimplemented. 
However I think having more control would be worth a lot.

Current behavior:
class C
{
     static int foo(int n) { return 1; }
     static int foo(ref int n) { return 2; }
}

// with forward
int bar()(auto ref int x) { return C.foo(forward!x); }

// without forward
int baz()(auto ref int x) { return C.foo(x); }

int i;
assert(bar(1) == 1);
assert(bar(i) == 2);

assert(baz(1) == 2);
assert(baz(i) == 2);

New behavior:
assert(bar(1) == 1);
assert(bar(i) == 1);
assert(bar(ref i) == 2); // <- needs implementation somehow though

assert(baz(1) == 1);
assert(baz(i) == 1);
assert(baz(ref i) == 1);

however you can now define
int foo()(auto ref int x) { return C.foo(ref x); }
assert(foo(1) == 2);
assert(foo(i) == 2);
assert(foo(ref i) == 2);


More information about the Digitalmars-d mailing list