Apart from the code cleanliness, another benefit that anonymous methods makes it easy to use local variables, this part I don't go further here, but it's also a great benefit.
Actually that is called a closure isn't it? And you do not need an anonymous method for a closure. You could use a nested named procedure instead, if closures were implemented (and if they were implemented using nested procs)
I noted that in every thread about anonymous methods. Half of the time (at least half of the time) what people were speaking about are closures.
It may well be that most (maybe all) other languages combine those 2 features. But that does not mean that one is the other.
- It is possible (ignoring if it is desirable or not) to have anonymous methods, that are not closures
- It is possible to have closures without anonymous methods.
So if asked about the benefit of anonymous methods, do not answer with the benefits of closures. That does not answer the question. If it is closures you want, then it can be done using a nested proc/func that is used to create the closure.
And using nested proc for closure, you would have 99.9% identical code, with the only difference:
- the source of the closure needs a name (that should not be a problem)
- the source will be declared a bit further up (as nested proc, instead of closure)
All else is identical.
So where is your code more clean, than the same code but using a nested procedure (from which, if fpc supports it, you create your closure)?
procedure Foo;
var
// locals, that can be used in closure
procedure CallBackFordoSomethingInTheMainProcess; IsClosure; // some modifier fpc needs to add
begin
doSomethingInTheMainProcess;
end;
begin
//...
js :=
'(function(){' +
' /*...*/' +
' srcCode = doSomethingWith(srcCode, options);' +
' hostApp.SendStringValueToHostApp("%s", srcCode);' +
'})();';
FJsEngin.ExecJavaScriptInSeparatedProcess(js,
(*The anonymous method to be executed once the JS code is executed asynchronously in another process and returns the value.
*)
@CallBackFordoSomethingInTheMainProcess
);