digitalmars.D.learn - ref lost to opDispatch
- Carl Sturtivant (19/19) Jul 27 2013 I ran into the following behavior.
- Namespace (4/23) Jul 27 2013 void opDispatch(string f, Args...)(auto ref Args args) {
- bearophile (19/22) Jul 27 2013 Is this enough?
- Artur Skawina (3/27) Jul 27 2013 void opDispatch(string f, Args...)(auto ref Args args)
- Carl Sturtivant (6/7) Jul 27 2013 What exactly is the effect of this? Does it make all of args ref
- Carl Sturtivant (4/11) Jul 27 2013 And, can anyone please point me at the online documentation at
- Artur Skawina (3/13) Jul 27 2013 http://dlang.org/template.html#auto-ref-parameters
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
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
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
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
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
On Saturday, 27 July 2013 at 16:15:48 UTC, Carl Sturtivant wrote: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.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
On 07/27/13 18:23, Carl Sturtivant wrote:On Saturday, 27 July 2013 at 16:15:48 UTC, Carl Sturtivant wrote:http://dlang.org/template.html#auto-ref-parameters arturAnd, 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.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