Thread overview
ref lost to opDispatch
Jul 27, 2013
Carl Sturtivant
Jul 27, 2013
Namespace
Jul 27, 2013
bearophile
Jul 27, 2013
Artur Skawina
Jul 27, 2013
Carl Sturtivant
Jul 27, 2013
Carl Sturtivant
Jul 27, 2013
Artur Skawina
July 27, 2013
I ran into the following behavior.

==============================================
void main() {
	int x = 100;

	B().modify( x, 99);
	writeln( x); //still 100

	modify( x, 99);
	writeln( x); //now is 99
}

void modify( ref int x, int y) { x = y; }

struct B {
	void opDispatch(string f, Args...)( Args args) {
		mixin( f~"(args);");
	}
}
==============================================

When dispatched through a general argument tuple, ref is lost and the dispatched code therefore does not have the desired side effect. Is there anything I can do about this?
July 27, 2013
On Saturday, 27 July 2013 at 15:09:44 UTC, Carl Sturtivant wrote:
>
> I ran into the following behavior.
>
> ==============================================
> void main() {
> 	int x = 100;
>
> 	B().modify( x, 99);
> 	writeln( x); //still 100
>
> 	modify( x, 99);
> 	writeln( x); //now is 99
> }
>
> void modify( ref int x, int y) { x = y; }
>
> struct B {
> 	void opDispatch(string f, Args...)( Args args) {
> 		mixin( f~"(args);");
> 	}
> }
> ==============================================
>
> When dispatched through a general argument tuple, ref is lost and the dispatched code therefore does not have the desired side effect. Is there anything I can do about this?

void opDispatch(string f, Args...)(auto ref Args args) {
    mixin( f~"(args);");
}
July 27, 2013
Carl Sturtivant:

> When dispatched through a general argument tuple, ref is lost and the dispatched code therefore does not have the desired side effect. Is there anything I can do about this?

Is this enough?


struct Foo {
    void opDispatch(string f, Args...)(ref Args args) {
        mixin(f ~ "(args);");
    }
}

void bar(ref int y) {
    y--;
}

void main() {
    int x = 100;
    Foo().bar(x);
    assert(x == 99);
    bar(x);
    assert(x == 98);
}


Bye,
bearophile
July 27, 2013
On 07/27/13 17:09, Carl Sturtivant wrote:
> 
> I ran into the following behavior.
> 
> ==============================================
> void main() {
>     int x = 100;
> 
>     B().modify( x, 99);
>     writeln( x); //still 100
> 
>     modify( x, 99);
>     writeln( x); //now is 99
> }
> 
> void modify( ref int x, int y) { x = y; }
> 
> struct B {
>     void opDispatch(string f, Args...)( Args args) {
>         mixin( f~"(args);");
>     }
> }
> ==============================================
> 
> When dispatched through a general argument tuple, ref is lost and the dispatched code therefore does not have the desired side effect. Is there anything I can do about this?

   void opDispatch(string f, Args...)(auto ref Args args)

artur
July 27, 2013
>    void opDispatch(string f, Args...)(auto ref Args args)

What exactly is the effect of this? Does it make all of args ref parameters when instantiated?

In which case it doesn't solve my real problem where I have more that 100 functions with all sorts of parameter lists being dispatched and I only want ref where it's prescribed.

Is the behavior of the code I wrote a design feature or a bug?
July 27, 2013
On Saturday, 27 July 2013 at 16:15:48 UTC, Carl Sturtivant wrote:
>>   void opDispatch(string f, Args...)(auto ref Args args)
>
> What exactly is the effect of this? Does it make all of args ref parameters when instantiated?
>
> In which case it doesn't solve my real problem where I have more that 100 functions with all sorts of parameter lists being dispatched and I only want ref where it's prescribed.
>
> Is the behavior of the code I wrote a design feature or a bug?

And, can anyone please point me at the online documentation at the right place to tell me enough to infer what placing "auto ref" in front of the argument tuple does.
July 27, 2013
On 07/27/13 18:23, Carl Sturtivant wrote:
> On Saturday, 27 July 2013 at 16:15:48 UTC, Carl Sturtivant wrote:
>>>   void opDispatch(string f, Args...)(auto ref Args args)
>>
>> What exactly is the effect of this? Does it make all of args ref parameters when instantiated?
>>
>> In which case it doesn't solve my real problem where I have more that 100 functions with all sorts of parameter lists being dispatched and I only want ref where it's prescribed.
>>
>> Is the behavior of the code I wrote a design feature or a bug?
> 
> And, can anyone please point me at the online documentation at the right place to tell me enough to infer what placing "auto ref" in front of the argument tuple does.

http://dlang.org/template.html#auto-ref-parameters

artur