开发者

Delphi local functions

开发者 https://www.devze.com 2023-02-24 10:27 出处:网络
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 t

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?


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.


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).

0

精彩评论

暂无评论...
验证码 换一张
取 消