Now the question is, how do you call it? If you simply call | GetShowMethod;

It compiles and executes, but all it does is call the anonymous method assignment code, throwing away the anonymous method returned by the function.

How do you call the actual anonymous method passing a parameter to it? One option is to use a temporary anonymous method variable: var ip: TIntProc; begi n ip := GetShowMethod(); ip (3);

Notice in this case the parenthesis after the GetShowMethod call. If you omit them (a standard Pascal practice) you'll get the following error: E20l0 Incompatible types: 'TIntProc' and 'Procedure'

Without the parenthesis the compiler thinks you want to assign the GetShowMethod function itself, and not its result to the ip method pointer. Still, using a temporary variable might not be the best option in this case, as is makes the code unnaturally complex. A simple call | GetShowMethod(3);

won't compile, as you cannot pass a parameter to the method. You need to add the empty parenthesis to the first call, and the Integer parameter to the resulting anonymous method. Oddly enough, you can write: | GetShowMethod()(3);

An alternative solution is to use the internal implementation of anonymous methods, and call the low-level Invoke methods that gets added by the compiler (in which case you can omit the empty parenthesis): | GetShowMethod.Invoke (3);

Was this article helpful?

0 0
Project Management Made Easy

Project Management Made Easy

What you need to know about… Project Management Made Easy! Project management consists of more than just a large building project and can encompass small projects as well. No matter what the size of your project, you need to have some sort of project management. How you manage your project has everything to do with its outcome.

Get My Free Ebook

Post a comment