Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't inline a function?

Tags:

inline

delphi

I declared a few functions only for legibility, and I want to check if the compiler is inlining them. According to this answer I figured I could mark them inline and get a hint if they are not inlined, however, when I try to do so I get the following error:

[dcc32 Error] MyClass.pas(266): E1030 Invalid compiler directive: 'INLINE'

So I tried it with a simple function:

procedure TMyClass.Swap(var a, b : Integer); inline;
var
  c : Integer;
begin
  c := a;
  a := b;
  b := c;
end;

Alas, I get the same error. According to the docs the default is {$INLINE ON}, so I assumed I only had to add inline;. Nevertheless I tried declaring {$INLINE ON}, to no avail. My Google-fu failed me, so here I am.

I am using Delphi 10.1 Berlin.

like image 488
afarah Avatar asked Sep 01 '25 11:09

afarah


1 Answers

You're putting it on the implementation, not the declaration. Putting it on the implementation will work for standalone functions and procedures, but not for class methods. Those must be defined as inline in the declaration itself.

interface

type
  TMyClass = class(TObject)
  private
    procedure Swap(var a, b: integer); inline;
  end;

implementation 

procedure TMyClass.Swap(var a, b:integer);
begin
  //
end;
like image 184
Ken White Avatar answered Sep 04 '25 09:09

Ken White