SRL Archive - Lape

'out' parameter type value not discarded & variable unused hint


riwu

2014-09-16 19:48:32

Currently 'out' behaves the same as 'var' as it does not discard the value of the argument.

procedure a(out x: integer);
begin
end;

var
x: integer;

begin
x := 5;
a(x);
writeln(x);
end.



Also possible to add variable unused code hint for lape? (the way PS did)
@nielsie95

riwu

2014-09-17 02:49:01

Another minor problem:

var
a: TCardinalArray;

procedure TCardinalArray.test();
begin
writeln(self);
end;

procedure b(const x: TCardinalArray);
begin
x.test;
end;

begin
b(a);
end.

Making reference to self is illegal if it's a constant? Shouldn't it work as long as we dont change the value of self?

Brandon

2014-09-17 04:29:56

From: riwu
Currently 'out' behaves the same as 'var' as it does not discard the value of the argument.


What? Those statements are contradictory. Out is an alias for var.. It DOES discard the value of the argument upon assignment. Your "A" function does nothing so of course it prints the same value. Try changing the parameter's value in "A".



From: riwu
Another minor problem.. Making reference to self is illegal if it's a constant? Shouldn't it work as long as we dont change the value of self?


https://villavu.com/forum/showthread.php?t=110125&p=1307353#post1307353


You can see that self is nil:


var
a: TCardinalArray;

procedure TCardinalArray.test();
begin
writeln(@self);
end;

procedure b(const x: TCardinalArray);
begin
x.test;
end;

begin
b(a);
end.

So.. in this case, you can't use it.

slacky

2014-09-17 05:49:22

From: riwu
Currently 'out' behaves the same as 'var' as it does not discard the value of the argument.

...

Expanding on what Brandon said:
Only real difference is that out is "supposed to" tell the compiler that the given variable does not have to be initialized.

Doing this in Free Pascal:

procedure A(var i:Int32);
begin end;

var x:Int32;
begin
A(x);
end.
Will show a notice:
>> Hint: Variable "x" does not seem to be initialized
Doing the exact same but using out will not show this notice..
That's all there is to it.

Since Lape does not give any hints, and always initializes variables for us, I would assume that out is just an alias for var… So, out is just a hint for scripters :P

riwu

2014-09-18 03:44:38

ahh, when i read about other languages in which the existing value of an out argument is not accessible, i thought it meant that its value is set to null at the start. Guess they only disallow accessing it.
The current behavior is right then. I'd just set it to null at the start myself.
Thanks for the explanation.

Brandon

2014-09-18 07:48:02

From: riwu
ahh, when i read about other languages in which the existing value of an out argument is not accessible, i thought it meant that its value is set to null at the start. Guess they only disallow accessing it.
The current behavior is right then. I'd just set it to null at the start myself.
Thanks for the explanation.

The existing value of an out variable is always accessible and you can always change the variable's value.

No need to set it nil. Lape already initialises all variables. Integerals to "0". Pointers to "nil", Arrays to "[]", etc..