是否可以在Delphi中创建一个类型方法?

时间:2020-12-26 19:17:31

It's possible to create a type idenitifier for example:

例如,可以创建一个类型标识符:

type PByte = ^Byte;

Is it possible to do the following:

是否可以执行以下操作:

function a:shortint;
begin
  Exit(8);
end;

type b = a;

so you can call "b" and "a". Is it possible?

所以你可以叫“b”和“a”。可能吗?

I'm asking,because I'd like to replace "Exit" with "return" so I can call this:

我问,因为我想用“返回”替换“退出”,所以我可以这样称呼:

return(5);// for example

2 个解决方案

#1


It seems to me you are confusing function types and definitions. You can create type for methods and functions, and you use them every day in Delphi, such as TNotifyEvent, which is the type of methods that is called on most operations with user controls. Such types allow you to define functions corresponding with a certain header (i.e. expected parameters and return value).

在我看来,你是混淆功能类型和定义。您可以为方法和函数创建类型,并且每天在Delphi中使用它们,例如TNotifyEvent,这是在大多数具有用户控件的操作中调用的方法类型。这些类型允许您定义与某个标题相对应的函数(即预期参数和返回值)。

A whole different issue is function pointers - a pointer to a specific instance of a function, so that you can "call" the pointer and it will invoke the function. The pointer may be of a function of a certain type (as described above), but the two issues have practically nothing to do with one another.

一个完全不同的问题是函数指针 - 指向函数的特定实例的指针,以便您可以“调用”指针并调用该函数。指针可以是某种类型的函数(如上所述),但这两个问题实际上彼此无关。

A third totally unrelated thing is a call-stack of functions. The Exit, as mentioned by Tobias, is a reserved compiler directive and not a function per se.

第三个完全不相关的东西是函数的调用堆栈。 Tobias提到的Exit是一个保留的编译器指令,而不是一个函数本身。

To conclude, for all practical purposes what you want to achieve is not right and is not possible. You may be able to "cheat" Delphi into accepting something like that, but it would just be wrong IMHO.

总而言之,出于所有实际目的,您想要实现的目标是不对的,也是不可能的。你或许可以“欺骗”Delphi接受类似的东西,但这只是错误的恕我直言。

#2


You should use at least Delphi 2009. There is new Exit(Result) construct.

你应该至少使用Delphi 2009.有一个新的Exit(Result)结构。

If you prefer stick with the old Delphi's version, you should check this out. There is no ready functionality for Exit(Result), but it is very easy to implement by using plugins.

如果你更喜欢坚持使用旧的Delphi版本,你应该检查一下。 Exit(Result)没有现成的功能,但使用插件很容易实现。

#1


It seems to me you are confusing function types and definitions. You can create type for methods and functions, and you use them every day in Delphi, such as TNotifyEvent, which is the type of methods that is called on most operations with user controls. Such types allow you to define functions corresponding with a certain header (i.e. expected parameters and return value).

在我看来,你是混淆功能类型和定义。您可以为方法和函数创建类型,并且每天在Delphi中使用它们,例如TNotifyEvent,这是在大多数具有用户控件的操作中调用的方法类型。这些类型允许您定义与某个标题相对应的函数(即预期参数和返回值)。

A whole different issue is function pointers - a pointer to a specific instance of a function, so that you can "call" the pointer and it will invoke the function. The pointer may be of a function of a certain type (as described above), but the two issues have practically nothing to do with one another.

一个完全不同的问题是函数指针 - 指向函数的特定实例的指针,以便您可以“调用”指针并调用该函数。指针可以是某种类型的函数(如上所述),但这两个问题实际上彼此无关。

A third totally unrelated thing is a call-stack of functions. The Exit, as mentioned by Tobias, is a reserved compiler directive and not a function per se.

第三个完全不相关的东西是函数的调用堆栈。 Tobias提到的Exit是一个保留的编译器指令,而不是一个函数本身。

To conclude, for all practical purposes what you want to achieve is not right and is not possible. You may be able to "cheat" Delphi into accepting something like that, but it would just be wrong IMHO.

总而言之,出于所有实际目的,您想要实现的目标是不对的,也是不可能的。你或许可以“欺骗”Delphi接受类似的东西,但这只是错误的恕我直言。

#2


You should use at least Delphi 2009. There is new Exit(Result) construct.

你应该至少使用Delphi 2009.有一个新的Exit(Result)结构。

If you prefer stick with the old Delphi's version, you should check this out. There is no ready functionality for Exit(Result), but it is very easy to implement by using plugins.

如果你更喜欢坚持使用旧的Delphi版本,你应该检查一下。 Exit(Result)没有现成的功能,但使用插件很容易实现。