Forum > General

Hint: Local variable xx of a managed type does not seem to be initialized

<< < (4/4)

wp:
My standard way to get rid of this hint is to initialize the variable in the declaration:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure Test;var  x : Array of integer = nil;  s: string = '';  ws: widestring = '';begin  SetLength(x, ...);  SetLength(s, ...);  SetLength(ws, ...);  ...  ...end;These are very natural and self-explaining additions and much clearer than the standard {%H} directive.

lucamar:

--- Quote from: wp on May 09, 2021, 01:30:50 pm ---My standard way to get rid of this hint is to initialize the variable in the declaration: [...]
--- End quote ---

Yes, I do the same. The code looks much cleaner than using directives. :)

PeterX:

--- Quote from: wp on May 09, 2021, 01:30:50 pm ---My standard way to get rid of this hint is to initialize the variable in the declaration:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure Test;var  x : Array of integer = nil;  s: string = '';  ws: widestring = '';begin  SetLength(x, ...);  SetLength(s, ...);  SetLength(ws, ...);  ...  ...end;These are very natural and self-explaining additions and much clearer than the standard {%H} directive.

--- End quote ---

Yes, this is self-explaining, and done at the root. Thumbs up !

But I today found some local vars that are records.
And I get this HINT ".. of a managed type does not seem to be initialized".
I have no idea how to solve this in a clean way ..

wp:
You can call Default() at the top of the routine (https://wiki.freepascal.org/FPC_New_Features_3.0.0#New_compiler_intrinsic_Default):


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---type  TData = record    a, b: Integer;  end; procedure Test;var  data: TData;begin  data := Default(TData);  ...

ASerge:

--- Quote from: wp on May 09, 2021, 01:30:50 pm ---
--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure Test;var  x : Array of integer = nil;  s: string = '';  ws: widestring = '';begin  SetLength(x, ...);  SetLength(s, ...);  SetLength(ws, ...);  ...  ...end;
--- End quote ---
By the way, this code will be without a hint, even if there is no initialization.
Sample project:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---{$APPTYPE CONSOLE}{$MODE OBJFPC} procedure UseVariable(var V);begin  Writeln(HexStr(@V));end; procedure Test1Hint;var  A: TBoundArray;begin  UseVariable(A); // project1.lpr(13,16) Hint: Local variable "A" of a managed type does not seem to be initializedend; procedure Test1_1NoHint;var  A: TBoundArray;begin  SetLength(A, 10); // Set the length first (and there is no hint in this line!)  UseVariable(A);   // No hintend; procedure Test1_2NoHint;var  A: TBoundArray = nil; // Initialize in the declarationbegin  UseVariable(A); // No hintend; procedure Test2Hint;var  R: record    A: TBoundArray;  end;begin  UseVariable(R.A); // project1.lpr(37,15) Hint: Local variable "R" of a managed type does not seem to be initializedend; procedure Test2_1NoHint;var  R: record    A: TBoundArray;  end;begin  SetLength(R.A, 10); // Set the length first  UseVariable(R.A); // No hintend; procedure Test2_2NoHint;var  R: record    A: TBoundArray;  end;begin  R.A := nil; // Initialize explicitly  UseVariable(R.A); // No hintend; procedure Test2_3NoHint;type  TR = record    A: TBoundArray;  end;var  R: TR;begin  R := Default(TR); // Initialize with the Default  UseVariable(R.A); // No hintend; begin  Test1Hint;  Test1_1NoHint;  Test1_2NoHint;  Test2Hint;  Test2_1NoHint;  Test2_2NoHint;  Test2_3NoHint;  ReadLn;end.

Navigation

[0] Message Index

[*] Previous page

Go to full version