www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - ref lost to opDispatch

reply "Carl Sturtivant" <sturtivant gmail.com> writes:
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?
Jul 27 2013
next sibling parent "Namespace" <rswhite4 googlemail.com> writes:
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);"); }
Jul 27 2013
prev sibling next sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
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
Jul 27 2013
prev sibling parent reply Artur Skawina <art.08.09 gmail.com> writes:
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
Jul 27 2013
parent reply "Carl Sturtivant" <sturtivant gmail.com> writes:
    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?
Jul 27 2013
parent reply "Carl Sturtivant" <sturtivant gmail.com> writes:
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.
Jul 27 2013
parent Artur Skawina <art.08.09 gmail.com> writes:
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
Jul 27 2013