Delphi: function Result not emptied during for loop -
is normal?
for := 1 10 x.test; x.test; x.test; x.test; function test: string; begin {$ifdef debug} debugmessage('result check = '+result,3); {$endif} result := result + 'a'; end; 10:39:59: result check = 10:39:59: result check = 10:39:59: result check = aa 10:39:59: result check = aaa 10:39:59: result check = aaaa 10:39:59: result check = aaaaa 10:39:59: result check = aaaaaa 10:39:59: result check = aaaaaaa 10:39:59: result check = aaaaaaaa 10:39:59: result check = aaaaaaaaa 10:39:59: result check = 10:39:59: result check = 10:39:59: result check =
function result stack not freed during loop? :o
result
treated implicit var
parameter function.
imagine if wrote out explicitly way:
procedure test(var result: string); begin result := result + 'a'; end; := 1 10 test(s);
then expect append s
.
the fact throwing away result
each time call why compiler decides finalise it. @gabr points out, elects not finalize implicit variable when inside loop optimisation.
if assign result of test
string every time called test
you'd see string longer each time, never re-initialized.
this why should initialize result variable. looks local variable, best thought of var
parameter.
Comments
Post a Comment