Thread overview
default values for inout parameters
Dec 05, 2006
Charlie
Dec 05, 2006
BCS
Dec 06, 2006
Charlie
Dec 06, 2006
BCS
Dec 06, 2006
Charlie
December 05, 2006
The following fails with : Error: "" is not an lvalue

void f( inout char [] x = "" ){ }

void main ()
{
  f();
}


Why is this not allowed ?
December 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);}
December 06, 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);}
December 06, 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[]
> 
> 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."

December 06, 2006
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