Delphi Compiler Directive to Evaluate Arguments in Reverse

前端 未结 4 937
我寻月下人不归
我寻月下人不归 2021-01-13 11:41

I was really impressed with this delphi two liner using the IFThen function from Math.pas. However, it evaluates the DB.ReturnFieldI first, which is unfortunate because I n

4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-13 11:50

    The calling convention affects the way they are evaluated.
    There is not a compiler define to control this.

    Pascal is the calling convention you would have to use to get this behavior.

    Although I would personally never depend on this type of behavior.

    The following example program demonstrates how this works.

    program Project2;
    {$APPTYPE CONSOLE}
    uses SysUtils;
    
    function ParamEvalTest(Param : Integer) : Integer;
    begin
      writeln('Param' + IntToStr(Param) + ' Evaluated');
      result := Param;
    end;
    
    procedure TestStdCall(Param1,Param2 : Integer); stdCall;
    begin
      Writeln('StdCall Complete');
    end;
    
    procedure TestPascal(Param1,Param2 : Integer); pascal;
    begin
      Writeln('Pascal Complete');
    end;
    
    procedure TestCDecl(Param1,Param2 : Integer); cdecl;
    begin
      Writeln('CDecl Complete');
    end;
    
    procedure TestSafecall(Param1,Param2 : Integer); safecall;
    begin
      Writeln('SafeCall Complete');
    end;
    
    begin
      TestStdCall(ParamEvalTest(1),ParamEvalTest(2));
      TestPascal(ParamEvalTest(1),ParamEvalTest(2));
      TestCDecl(ParamEvalTest(1),ParamEvalTest(2));
      TestSafeCall(ParamEvalTest(1),ParamEvalTest(2));
      ReadLn;
    end.
    

    This would require you to write your own IfThen Functions.

    If you really want this to be a one liner you really can do that in Delphi. I just think it looks ugly.

    If (DB.First = 0) then result :=  DB.ReturnFieldI('awesomedata1') else result := 0;
    

提交回复
热议问题