How to determine the type of procedure that works for the local procedure?

I am creating a request queue in the TMyThread user thread, and I am having difficulty determining the type of procedure that can be used for the routine. I have a record that represents a query, the corresponding record pointer and the type of procedure that is used in the record, and uses the record pointer ...

 type PRequest = ^TRequest; TResponseProc = procedure(Sender: TMyThread; Request: PRequest); TRequest = record Request: String; Proc: TResponseProc; Response: String; end; 

The problem is that when I implement a routine called ResponseProc and try to assign ResponseProc to TResponseProc , it does not work, and the IDE returns this error message:

 [DCC Error] MyProject.dpr(42): E2094 Local procedure/function 'ResponseProc' assigned to procedure variable 

How to define this type of TResponse procedure and use it with a routine?

+4
source share
1 answer

Records of records and procedures are in order. The error message indicates that you are using a local procedure that is defined inside the scope of another function. You cannot use pointers to such functions because they require additional work to call, which cannot be expressed in a regular function pointer. (The compiler forbids creating pointers to functions that the caller does not know how to use.)

The solution is to move your function outside of any other function in which you defined it. If this is difficult to do because the inner function uses variables from the outer function, then you will have to figure out another way to get their values ​​to another function, for example, passing them as parameters, possibly making them additional members of this query record.

Another option is to use a reference to the procedure, and then instead define the local procedure as anonymous. It can access local variables, although only Delphi and C ++ Builder will know how to call it, so it is not an option if you need external API compatibility.

+8
source

All Articles