Recent

Author Topic: FPC feature suggestion  (Read 2487 times)

440bx

  • Hero Member
  • *****
  • Posts: 6081
FPC feature suggestion
« on: December 25, 2018, 07:18:14 am »
hello,

FPC is very good about issuing hints mentioning the fact that some variable being used may not have been initialized.  This can be very useful.

Unfortunately, at this time, there is no way to tell FPC that either, the variable is already initialized or, that it will be initialized by the procedure/function it is being passed to.  For instance, consider the code snippet below:
Code: Pascal  [Select][+][-]
  1. function SomeFunc(parm1 : integer; var parm2 : somerecord) : Boolean;
  2. begin
  3.    RtlZeroMemory(@parm2, sizeof(parm2));  // initialize parm2
  4.   { perform binary magic here }
  5.   result := everything_is_great;
  6. end;
  7.  
  8. ...
  9. ...
  10. ...
  11. var
  12.   MyParm : somerecord;
  13.  
  14. begin
  15.   { use SomeFunc }
  16.  
  17.   r := SomeFunc(v, MyParm);   { compiler issues a hint that MyParm is not initialized }
  18. end.
  19.  

It would be very nice to have something along the lines of
Code: Pascal  [Select][+][-]
  1. {$initializes parm2}
that could be placed before SomeFunc's begin statement to inform the compiler that SomeFunc is responsible for initializing parm2.  That way, the compiler would not emit superfluous hints about the variable not being initialized every time SomeFunc is used in the program's body.

Thanks for reading. :)



FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

bytebites

  • Hero Member
  • *****
  • Posts: 777
Re: FPC feature suggestion
« Reply #1 on: December 25, 2018, 09:15:36 am »
Does this help?
Code: Pascal  [Select][+][-]
  1. function SomeFunc(parm1 : integer; out parm2 : somerecord) : Boolean;

Thaddy

  • Hero Member
  • *****
  • Posts: 18729
  • To Europe: simply sell USA bonds: dollar collapses
Re: FPC feature suggestion
« Reply #2 on: December 25, 2018, 09:39:38 am »
You can suppress the message with {$push}{$warn xxxx off}.. declaration --- {$pop} xxxx is the number of the warning.
Also note rtlxxx fiunctions are platform. Why not
Code: Pascal  [Select][+][-]
  1. parm2 := Default(SomeRecord);
« Last Edit: December 25, 2018, 10:18:51 am by Thaddy »
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

ASerge

  • Hero Member
  • *****
  • Posts: 2475
Re: FPC feature suggestion
« Reply #3 on: December 25, 2018, 11:28:00 am »
Code: Pascal  [Select][+][-]
  1. function SomeFunc(parm1 : integer; out parm2 : somerecord) : Boolean;
+1

440bx

  • Hero Member
  • *****
  • Posts: 6081
Re: FPC feature suggestion
« Reply #4 on: December 25, 2018, 12:53:14 pm »
Does this help?
Code: Pascal  [Select][+][-]
  1. function SomeFunc(parm1 : integer; out parm2 : somerecord) : Boolean;
Not only that helps, that is the perfect way of doing it.

Thank you, I much appreciate your pointing out the right way of doing it.
FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

 

TinyPortal © 2005-2018