afarah
afarah

Reputation: 783

Can't inline a function?

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.

Upvotes: 4

Views: 963

Answers (1)

Ken White
Ken White

Reputation: 125679

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;

Upvotes: 6

Related Questions