Link to home
Start Free TrialLog in
Avatar of Westley
Westley

asked on

inline methods in Delphi 3

How do I use the reserved word "inline" in Delphi 3?
I would also like to know if there is more than one way to use inline methods, like in C++ - you can write the implementation in the declaration part (.h file), or type "inline" and implement the method in the definition part (.cpp file).

Thanx, Westley.
Avatar of dionysos_swamp
dionysos_swamp

inline is not the same as in C++ it's a stupid way of throwing in hand-assembled code into the program, it's not really usefull as the asm .. end; can assemble everything now, it's a relic from Turbo Pascal.

an example:
 inline($cd, $19);

this would reboot (maybe) the PC under DOS-TP

... Afik


Avatar of Westley

ASKER

I read in a Delphi 2 book that Borland no longer supports inlines. It's still a reserved word, but it doesn't do anything.

But, OTOH, I heard there were inline methods in Turbo Pascal 7, and Borland said they keep backward compatibility. So who's right?
The question has a simple anwer: You can not use the Inline directive in Delphi 3.0 (nor in the earlier versions of Delphi).

The word Inline is reserved but not understood by the compiler. This means that can not use it in the expected way:

Procedure Foo;  Inline;
Begin ... End;

nor can you define you own identifier called Inline.

So, what do you do in stead? Well, the closest you can come to an Inline procedure is to use the $INCLUDE compiler directive.
Suppose you want to make an inline procedure with a number of statements:

Procedure Foo;  Inline;
Begin
  Statement1;
  ...
  StatementN
End;

While you can not do this directly, you can create a file (e.g. FOO.PAS) containing the statements from the procedure body.  Now, at every point in your source code where you would like to 'call' this procedure, you put the command {$INCLUDE FOO.PAS}.

How this will help you.

AndersWP
Avatar of Westley

ASKER

AndersWP,

This still doesn't help me with the biggest advantage of the inline concept: The ability to use private members outside the class.
For example, in C++, if you have declared
  int X;
inside a class, and it's private, you can still access it using
  int GetX() { return(X); }
and you don't get the overhead of using a real function.
Now Delphi supports properties, but I don't think properties are implemented inline, and since I'm using some private fields and exporting them via one-line-methods, I would like to know that I don't pay a high price for them.
If the purpose of the excercise is to expose private fields in a controlled manner, properties is the way to go. The Delphi analog of your C++ example would be:

TMyClass = Class
Private
  FX: Integer;    
Public
  Property X: Integer Read FX;    
End;

Note that there is no Get function involved - you access the variable directly without the function call overhead.

Greetings,
AndersWP
Avatar of Westley

ASKER

Sorry I have to reject it again.

As I already wrote, I know about properties, but I need to know how they are implemented - are they like a macro, or a full function call?

My class looks like this:

TPrivClass = class
  public
    function func1(S: String): Boolean;
    function func2(S: String): Boolean;

MyClass = class
  private
    PrivClass: TPrivClass;
...
end;

I want people to be able to use func2 only, therefore I made the class private. But I can't just write
  property funcy1[S; String]: // referring to MyFunc2
because I want to do some validity check on S, say - lowercase it. So I add:

function MyClass.MyFunc2(S:String): Boolean;
begin
  Result := PrivClass.func2(AnsiLowerCase(S));
end;

but now I'm adding the overhead of calling a function.
And I have dozens of those things all around my project.
Even worse - sometimes my property calls another property of the private class, so I end up having 3 or 4 one-line functions calling each other.

Hope this explains my problem, and thanks for the effort,

  Westley.
ASKER CERTIFIED SOLUTION
Avatar of AndersWP
AndersWP

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial