简体   繁体   English

我可以使用类型声明函数吗

[英]Can I declare a function using a type

I have a program which can use any combination of five DLLs to do some work. 我有一个程序,可以使用五个DLL的任意组合来完成某些工作。 All the DLLs have the same interface, same exported functions and procedures, and only differ in what they actually do. 所有DLL具有相同的接口,相同的导出函数和过程,只是它们的实际作用不同。

In order to make things easier, sanity-check-wise, I'd like to define the functions only once and then share the definition unit between anything. 为了简化操作,我想只定义一次函数,然后在任何东西之间共享定义单元。 The problem then would be to define a function of a given type. 那么问题将是定义给定类型的函数。

Imagine: 想像:

type
  TMyFunc = function(inP : Integer): outP : Boolean;

Can I now define a function called, say, doit , and force its definition to be of type TMyFunc , without redefining everything? 我现在是否可以定义一个函数,例如doit ,并强制其定义为TMyFunc类型,而无需重新定义所有内容?

I can't just type: 我不能只输入:

function doit: TMyFunc;

Is there any way to do this? 有什么办法吗?

It would really make defining everything easier, more secure and so on. 这确实会使定义一切变得更加容易,更安全等。

Right now, everything is defined several times, and making a necessary interface change at one point means that I need to remember to go everywhere and duplicate the change (and duplication is bad). 现在,所有事情都定义了好几次,并且在某一时刻进行了必要的接口更改,这意味着我需要记住四处走动并重复更改(重复很不好)。 If I could do the declaration in one place only then if I forgot to make a change somewhere compilation would fail, which would be great. 如果我只能在一个地方进行声明,那么如果我忘记进行更改,则编译会失败,那就太好了。

Is there any way to do this? 有什么办法吗?

The answer is, quite simply, no you cannot do this. 答案很简单,不,你不能这样做。 When you declare a function, you must include its complete argument list, return type, etc. 声明函数时,必须包括其完整的参数列表,返回类型等。

THIS is wrong: 这是错误的:

type
  TMyFunc = function(inP : Integer): outP : Boolean;

a function cannot have two return types (both outP and Boolean) ! 一个函数不能有两个返回类型(outP和Boolean)!

INSTEAD: 代替:

type
    outP = Longint; // or whatever you want outP to be.

    TMyFunc1 = function (inP : Integer): outP;
    TMyFunc2 = function (inP : Integer): Boolean;

you can now define a function variable called, say, doit1, and force it's definition to be of type TMyFunc1. 您现在可以定义一个名为doit1的函数变量,并强制其定义为TMyFunc1类型。 likewise: doit2 is type TMyFunc2. 同样:doit2是TMyFunc2类型。

var
    doit1 : TMyFunc1;
    doit2 : TMyFunc2;

if you have functions: 如果您具有功能:

const
    DllName1 = 'MyFirstDLL.DLL';
    DllName2 = 'MySecondDLL.DLL';

function TestFunc1(inP : Integer): outP; external DllName1;

function TestFunc2(inP : Integer): Boolean; external DllName2;

then you can assign these functions to the placeholders doit1, doit2: 然后可以将以下功能分配给占位符doit1,doit2:

procedure AssignPlaceholders;
begin
    doit1 := TestFunc1;
    doit2 := TestFunc2;
end;

NOTE: You must declare the placeholder types (TMyFunc1, TMyFunc2) EXACTLY the same as the real functions, including specifiers such as stdcall, cdecl etc. 注意:您必须声明占位符类型(TMyFunc1,TMyFunc2)与真实函数完全相同,包括说明符,例如stdcall,cdecl等。

After you call AssignPlaceholders, you can call doit1 and/or doit2 just the same as any other function. 调用AssignPlaceholders之后,可以像调用任何其他函数一样调用doit1和/或doit2。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM