digitalmars.D.learn - default values for inout parameters
- Charlie (7/7) Dec 05 2006 The following fails with : Error: "" is not an lvalue
The following fails with : Error: "" is not an lvalue void f( inout char [] x = "" ){ } void main () { f(); } Why is this not allowed ?
Dec 05 2006
Charlie wrote:The following fails with : Error: "" is not an lvalue void f( inout char [] x = "" ){ } void main () { f(); } Why is this not allowed ?What would this do? void f( inout char [] x = "" ) { x = "world"; } It amounts to "take a literal reference to a static string and set it to something else". It doesn't really have a meaning. You could try shelling it by hand. void f() { char[] x = null; f(x);}
Dec 05 2006
Yea I was trying to abuse it, you're right it shouldn't work with default parameters, what I was really trying to do was something like char [] x = char[] Charlie BCS wrote:Charlie wrote:The following fails with : Error: "" is not an lvalue void f( inout char [] x = "" ){ } void main () { f(); } Why is this not allowed ?What would this do? void f( inout char [] x = "" ) { x = "world"; } It amounts to "take a literal reference to a static string and set it to something else". It doesn't really have a meaning. You could try shelling it by hand. void f() { char[] x = null; f(x);}
Dec 05 2006
Charlie wrote:Yea I was trying to abuse it, you're right it shouldn't work with default parameters, what I was really trying to do was something like char [] x = char[] CharlieAre you looking for some sort of "stuff in a throw away value" feature? void foo(int i, inout char[] foo = /** somthing **/ ) { foo = "hello"; } char[] a; foo(1, a); assert(a == "hello"); foo(1); // no side effects What would be nice would be something like "Non l-values used for inout parameters get passed as copies and discarded after use."
Dec 05 2006
BCS wrote:Charlie wrote:Yes thats what I was looking for :). CharlieYea I was trying to abuse it, you're right it shouldn't work with default parameters, what I was really trying to do was something like char [] x = char[] CharlieAre you looking for some sort of "stuff in a throw away value" feature? void foo(int i, inout char[] foo = /** somthing **/ ) { foo = "hello"; } char[] a; foo(1, a); assert(a == "hello"); foo(1); // no side effects What would be nice would be something like "Non l-values used for inout parameters get passed as copies and discarded after use."
Dec 05 2006