Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


ID: 16841, Local nested procedures as procedural parameters

by Robert Cerny Email: Anonymous


Local nested procedures as procedural parameters would be extremely usefull for iterators.
Compiler handling would be very similar to method parameters.
Download Details
FTP  download also available 0 bytes
CDN Login Required to Download. (You will be redirected to the login page if you click on the Download Link)
To download this, you must have registered:
A free membership

For Delphi, Version 5.0  to 7.0 12 downloads
Copyright: No significant restrictions


Size: 0 bytes
Updated on Tue, 01 Apr 2003 14:06:44 GMT
Originally uploaded on Mon, 29 Oct 2001 13:58:31 GMT
Description
Local nested procedures as procedural parameters would be extremely usefull for iterators.
Compiler handling would be very similar to method parameter, instead that data field would contain caller's EBP.

Example:
type
TActionFunc = nested function(const S:string; Obj:pointer):integer;
//this type is something like TMethod
TNestedFunction = packed record
CodePtr:pointer;
FramePtr:pointer;
end;


function TStrings.FirstThat(Action:TActionFunc):integer;
begin
for Result := 0 to Count-1 do
if Action(Strings[Result],Objects[Result])<>0 then
Exit;
//to call Action, compiler would have to generate code like:
//
// PUSH [Action].FramePtr
// CALL [Action].CodePtr
// ADD ESP,4
// OR EAX,EAX
// JNE @@Exit
// JMP @NextIteration
Result := -1;
end;


Example use:

procedure TMyForm.Blabla;
var AString : string;
I : integer;

function SomeAction(const S:string; Obj:pointer):integer;
begin
//even if this is callback, it can access local variables like AString
or Self
if then Result := 1
else Result := 0;
end;

begin
I := SomeStrings.FirstThat(SomeAction);
//here compiler would have to generate code like:
// hidden local var Action:TNestedFunction
// MOV [Action].CodePtr,@SomeAction
// MOV [Action].FramePtr,EBP
// call FirstThat method with address of Action as parameter
end;

This would work correctly even if FirstThat method was recursive.
I can do this right now with some ASM code, but I have to redo that on every place, or call out and add some more overhead, but even in this case I have to do ASM code block for each type of procedure (parameters).
This would be really simple to build into compiler, and we could take
advantage of compiler's type checking, like procedures or methods.

   Latest Comments  View All Add New

Move mouse over comment to see the full text

Could not retrieve comments. Please try again later.

Server Response from: ETNACDC03