Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delphi local functions

I have a library of functions contained in a delphi unit which we shall call UtilitiesU. Some of these functions are just helper functions that are only used inside UtilitiesU. I would like to limit the scope of these functions to UtilitiesU. These are the methods that I know of for doing this:

  1. Remove the declaration from the interface and move the function before its dependents in the implementation - messy, counter-intuitive order of function definitions, not always possible if there is e.g. mutual dependency
  2. Put all the functions into a static class (ala Java) and make them public or private as appropriate - too much boilerplate, convoluted
  3. Declare the helper functions local to the functions in which they are used - same problems as point 1

Ideally, I would like to do it the C/C++ way - that is, declare them as static in the implementation section. Is this possible? Is there a better way?

like image 820
Nicholas Grasevski Avatar asked Feb 24 '26 13:02

Nicholas Grasevski


1 Answers

You can still declare your functions like this:

implementation

procedure ShowMe;forward;

procedure TForm1.FormCreate(Sender: TObject);
begin
  ShowMe;
end;

procedure showMe;
begin
  ShowMessage('Hello');
end;

So you can put all your function right after the implementation in the order you want. Declare them forward, and define them further down anywhere you want.

Personnally, I kindda prefer declaring those method as class methods. (Lets call it, "name space friendly"). But end result is pretty much the same.

like image 107
Ken Bourassa Avatar answered Feb 26 '26 03:02

Ken Bourassa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!