Why don't I get the warning W1036 Variable "'MyStrings' might not have been initialized' if the StringsToDo parameter in DoSomething is defined as a var parameter? I get the warning if I remove the var. Thanks for looking.

Why don't I get the warning W1036 Variable "'MyStrings' might not have been initialized' if the StringsToDo parameter in DoSomething is defined as a var parameter? I get the warning if I remove the var. Thanks for looking.

program Project2;
{$APPTYPE CONSOLE}
uses
SysUtils,
classes;

procedure DoSomething(var StringsToDo: TStringList);
begin
//do nothing
end;

procedure Test;
var
MyStrings : TStringList;
begin
MyStrings.Free; {1036 warning if StringsToDo param is NOT var}
DoSomething(MyStrings);
end;

begin
try
Test;
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
end.

Comments

  1. Carlos Barreto Feitoza Filho The use of the uninitialized variable is before the call to the function. Whether or not the parameter is a var parameter is therefore irrelevant. That seems to be the point you missed.

    ReplyDelete
  2. David Heffernan Now I see... It is really interesting and really i do not noted this "detail". Thank you!

    Well, with the correct understanding of the question I only have suppositions about this compiler behaviour.

    I'm not sure, but I guess this happens because the warning is generated for the whole program, I mean, the focus on the warning is the program being compiled, as if it was a "monolithig" thing.

    I guess the compiler try to deal with the syntax inside the program, and on finish its checking, the compiler reports what is wrong with that program like a whole.

    On thinking that way, the warning seems to make more sense because, using the var paramenter, on ending the program compile, the MyStrings variable could have been assigned or not (the compiler is not sure, so, no warning). On the other hand, if the parameter do not is a var parameter, on ending the program compile, the MyStrings variable surely was not assigned. The compiler is sure about that, so the warning is shown.

    To easy the understanding about what I'm trying to say, mentally put the phrase "on the current program" on the end of the warning message: [dcc32 Warning] Project1(16).dpr: W1036 Variable 'MyStrings' might not have been initialized on the current program

    I really do not know how the compiler works, but thinking on it as a very complex thing, I would not stay surprised if my interpretation was correct. And, to end this discussion, I agree that this is very unintuitive

    ReplyDelete
  3. Carlos Barreto Feitoza Filho It's a compiler bug, nothing more, nothing less

    ReplyDelete

Post a Comment