Recent

Author Topic: Warnings & Hints  (Read 2555 times)

graemejc

  • New Member
  • *
  • Posts: 33
Warnings & Hints
« on: July 14, 2018, 04:20:18 pm »
(* This is trivial code to demonstrate a problem I've run into. When I try to compile this code, I get a warning

Hint: Variable 'c' does not seem to be initialised.

Which is untrue, it is initialised in the procedure Ask.

My question is how do I turn off hints and warnings for that sort of thing. Lazarus gives me several options to deal with this ...

Hide message by inserting IDE directive {%H-}
Hide message by inserting {$warn 5058 off} to unit "project1.lpr"
Hide with project option {-vm5058}

I guess I can use the first option but really don't want a whole heap of {%H-} directives spread through my code. And I think to use it effectively,  I'd have to rewrite

ask(c);

... as ...

{%H-} ask(c); {%H+}

I searched all the pdf files that came with the program and I cannot find any reference to 5058 though I can guess what it means. Is there a better way to acknowledge the hint, then turn the hint off. Also searching all the pdf files, I can't find any reference to %H and it's only through experimentation that I've been able to come to the above semi-solution.
*)

program Project1;
Uses
        Crt;
var
        c: char;.
        {----------------------------------------------------------------------}
        procedure Ask(var ch:char);
        begin
                ch:=readkey;
        end;
        {----------------------------------------------------------------------}
begin
        ask(c);
        writeln(c);
        readkey;.
end { Program Project1 } .

Phil

  • Hero Member
  • *****
  • Posts: 2737
Re: Warnings & Hints
« Reply #1 on: July 14, 2018, 04:27:05 pm »
(* This is trivial code to demonstrate a problem I've run into. When I try to compile this code, I get a warning

Hint: Variable 'c' does not seem to be initialised.

Which is untrue, it is initialised in the procedure Ask.

Change param to out:

procedure Ask(out ch:char);

Compiler is doing what it should do, which is warn you that you're passing in a variable that you didn't first initialize.


Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: Warnings & Hints
« Reply #2 on: July 14, 2018, 04:32:50 pm »
1: it is not initialized - you are wrong, it is only initialized in the procedure - , but you can declare it initialized:
Code: Pascal  [Select][+][-]
  1. var
  2.   c:char = #0;
2. you can also do this locally:
Code: Pascal  [Select][+][-]
  1. var
  2. {$push}{$warn 5058 off}
  3.   c: char;
  4. {$pop}
Note I would not recommend using IDE macro's for this.Change editor and it doesn't work. I would simply initialize it at declaration.
« Last Edit: July 14, 2018, 04:42:10 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

graemejc

  • New Member
  • *
  • Posts: 33
Re: Warnings & Hints
« Reply #3 on: July 14, 2018, 05:21:21 pm »
Thanks to Thaddy and Phil. Excellent solutions both. Much appreciated.

Addendum: Phil sorry but that doesn't seem to work at all.
Addendum 2: Sometimes it works ... I'm not sure why sometimes it does not appear to work. Does not seem to work when one procedure has a parameter declared as out and it is called by another procedure also declared as an out. I'll have to examine my code a bit more closely. I'll assume at this stage I've made a mistake ... just not sure where.
« Last Edit: July 14, 2018, 06:11:20 pm by graemejc »

 

TinyPortal © 2005-2018