How can I temporarily disable the “return value might be undefined” warning?

后端 未结 3 1248
無奈伤痛
無奈伤痛 2021-02-20 01:25

I want to disable a specific warning (W1035) in my code, since I think that the compiler is wrong about this warning:

function TfrmNagScreen.Run: TOption;
begin
         


        
3条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-20 01:58

    You can use a neat trick to fool the compiler. Define a library function as so:

    procedure Abort(var X);
    begin
      SysUtils.Abort;
    end;
    

    You can then write your function as:

    if ShowModal = mrOk then
      Result := TOption(rdgAction.EditValue)
    else
      Abort(Result)
    

    The compiler thinks you've written to Result since it's a var parameter and it stops bleating.

提交回复
热议问题