05.11.2011 22:42, Bernd пишет:
2011/10/25<michael.vancann...@wisa.be>:
targethread.queue(
procedure(targetobject:ttargetobject;a:integer;b:someobject;c:string)
begin
targetobject.destinationprocedure(a,b,c);
end;
Note how common this looks compared to the original.
One point is that you could do the above with a local (and named) procedure
as well, and still have all the context. At the very least you would not be
raping pascal's readability by putting a
complete procedure declaration inside a code block.
How would you preserve the context without finding some place
somewhere (on the heap) to store the current values of a, b, and c?
The natural alternative to a closure if you don't have them in an OO
language is instead of passing a closure you pass an object (this
pattern is called Functor). The object contains the function along
with some state. Of course in a static language this involves a lot of
boiler plate to define the needed classes with their methods (one
class definition for every different closure) but once you have done
this you can use it like the closure in the above example and its
usage is totally readable:
targetthread.queue(TMyFunctor.Create(targetobject, a, b, c));
_______________________________________________
fpc-pascal maillist - fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
But modern trend is to add closures into OO languages, because code
become more laconic.
_______________________________________________
fpc-pascal maillist - fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal