www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - default values for inout parameters

reply Charlie <charlies nowhere.com> writes:
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
parent reply BCS <BCS pathilink.com> writes:
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
parent reply Charlie <charlies nowhere.com> writes:
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
parent reply BCS <BCS pathilink.com> writes:
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[]
 
 Charlie
 

Are 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
parent Charlie <charlies nowhere.com> writes:
BCS wrote:
 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[]

 Charlie

Are 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."

Yes thats what I was looking for :). Charlie
Dec 05 2006