3

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.

afarah
  • 748
  • 3
  • 19
  • You're supposed to put this directive only at the method declaration. – Victoria Jun 11 '18 at 22:20
  • @Jerry, I don't know what it means. But it's an answer, actually. Short to be an answer post, but an answer :) I don't think you can refer to documentation here. Well yes, you can say that you can use that directive only at class method declaration, not at its implementation; and show a code example of it. – Victoria Jun 12 '18 at 01:33

1 Answers1

6

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;
Ken White
  • 123,280
  • 14
  • 225
  • 444
  • Note that the `inline` directive *CAN* be applied to BOTH declarations AND definitions. Per the [*latest* documentation](http://docwiki.embarcadero.com/RADStudio/en/Calling_Procedures_and_Functions_(Delphi)#Using_the_inline_Directive): "*The `inline` directive is used in function and procedure declarations **and definitions**, like other directives.*" They even provide example functions that are marked at both declaration and definition – Remy Lebeau Jun 11 '18 at 23:34
  • 3
    @Remy, standalone functions and procedures can have this directive at declaration and implementation. Not class methods. Class methods can have it only at declaration (that's what documentation doesn't cover). – Victoria Jun 11 '18 at 23:40
  • @RemyLebeau: Sure, you can **also** repeat it to the implementation section if you'd like, but it's required at the declaration or it raises a compiler error (as the poster stated, and as I just tested in Berlin). (Not that it has much anyway, as the compiler will inline everything it thinks it can without the keyword, as long as the default `{$INLINE ON}` is in effect.) – Ken White Jun 11 '18 at 23:49
  • @Ken, using `{$INLINE}` directive doesn't change anything. At method implementation you cannot use this directive. That's it. – Victoria Jun 11 '18 at 23:52
  • @Victoria: Yes, that's what I've said (twice now, once in my answer, and again in my comment to Remy). – Ken White Jun 11 '18 at 23:52
  • 1
    @Ken, sorry, I misread your comment (or it's been modified since then). Yet, I don't think it's a good way to link the documentation at this time as it considers only standalone functions and procedures, not methods. – Victoria Jun 11 '18 at 23:54