Thread overview |
---|
December 05, 2006 default values for inout parameters | ||||
---|---|---|---|---|
| ||||
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 Re: default values for inout parameters | ||||
---|---|---|---|---|
| ||||
Posted in reply to Charlie | 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 Re: default values for inout parameters | ||||
---|---|---|---|---|
| ||||
Posted in reply to BCS | 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 Re: default values for inout parameters | ||||
---|---|---|---|---|
| ||||
Posted in reply to Charlie | 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 Re: default values for inout parameters | ||||
---|---|---|---|---|
| ||||
Posted in reply to BCS | 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
|
Copyright © 1999-2021 by the D Language Foundation