3
votes

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?

2
Why don't you put these "private helper functions" into their own unit, and just DON'T use them anywhere else, but inside the unit that needs to call them. I really don't see a value in making these utility functions ONLY visible to the inside of utilitiesU? This seems to be a makework project.Warren P
Thats is what I wrote in my solution 2. I think it is a good idea. But if somebody "uses" this helper unit, he/she will get access to all functions. Maybe thats not what he wants.Rafael Colucci
please don't mark the first answer you see as "the answer", give it at least a couple of hours to see what comes out. Forward declaration is the obvious answer IMO, but you marked Rafael's one correct very quickly and missed it.Мסž

2 Answers

3
votes

I would do:

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

Btw, theres not way of declaring a static class in delphi as you do in other languages. Only var and methods can be static, not the entire class.

There are other 3 ways

1 - You can create a class an put all the helper code on it on its private section (maybe statics methods) and use it inside the UtilitiesU unit. Bu I would not go for it, the 1 method you wrote is the best, I think.

2 - You can separe all your helper code in another unit, lest call it UtilitiesHelper.

3 - You can mix 1 and 2, but using protected methods. Then you can easily hack the helper class in your UtilitiesU unit. Example:

In the UtilitiesHelper unit

TUtilitiesHelper = class
protected
   //all your methods here
end;

In the UtilitiesU unit

TUtilitiesHelperHack = class(TUtilitiesHelper)
end;

With that you can access protected methods. But I dont recommend it also.

EDIT

As Ken said, you can use:

implementation

procedure ShowMe;forward;

I just tested this myself and it worked. (thats new to me also, thanks Ken).

10
votes

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.