How to use procedural variables: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: How to use procedural variables
| Old | New | Line | |
|---|---|---|---|
| 1 | + | Copy the text below and it will demonstrate the use of procedural variables, this is a fully working program. You don't even need to understand how it works. The syntax is pretty simple. | |
| 2 | + | ||
| 3 | + | ``` pascal | |
| 4 | + | program Test; | |
| 5 | + | ||
| 6 | + | {$mode objfpc}{$H+} | |
| 7 | + | uses | |
| 8 | + | {$IFDEF UNIX}{$IFDEF UseCThreads} | |
| 9 | + | cthreads, | |
| 10 | + | {$ENDIF}{$ENDIF} | |
| 11 | + | Classes; | |
| 12 | + | ||
| 13 | + | // Make the Types the type corresponds to a function signature | |
| 14 | + | type | |
| 15 | + | TFuncNoArgsString = function(): String; | |
| 16 | + | TFuncOneArgsString = function(x: string): string; | |
| 17 | + | ||
| 18 | + | // Example functions | |
| 19 | + | function Hello: String; | |
| 20 | + | begin | |
| 21 | + | Result := 'Hello There'; | |
| 22 | + | end; | |
| 23 | + | ||
| 24 | + | function Woah(G: String): String; | |
| 25 | + | begin | |
| 26 | + | Result := 'Woah ' + G; | |
| 27 | + | end; | |
| 28 | + | ||
| 29 | + | // Overloaded function takes the two types of function | |
| 30 | + | // pointers created above | |
| 31 | + | procedure Take(f: TFuncNoArgsString); overload; | |
| 32 | + | begin | |
| 33 | + | WriteLn(f()); | |
| 34 | + | end; | |
| 35 | + | ||
| 36 | + | procedure Take(f: TFuncOneArgsString); overload; | |
| 37 | + | begin | |
| 38 | + | WriteLn(f('there!!!')); | |
| 39 | + | end; | |
| 40 | + | ||
| 41 | + | var | |
| 42 | + | ptr: Pointer; | |
| 43 | + | List: TList; | |
| 44 | + | begin | |
| 45 | + | // the "@" symbol turns the variable into a pointer. | |
| 46 | + | // This must be done in order pass a function as a | |
| 47 | + | // parameter. This also demonstrates that pascal | |
| 48 | + | // keeps track of the pointer type so the overloading works! | |
| 49 | + | ||
| 50 | + | Take(@Hello); | |
| 51 | + | Take(@Woah); | |
| 52 | + | ||
| 53 | + | // Now put a function in an untyped pointer | |
| 54 | + | ptr := @Hello; | |
| 55 | + | // Type the pointer and call it all at the same time | |
| 56 | + | WriteLn(TFuncNoArgsString(ptr)); | |
| 57 | + | // A TList Example | |
| 58 | + | List := TList.Create; | |
| 59 | + | List.Add(@Hello); | |
| 60 | + | WriteLn(TFuncNoArgsString(List[0])); | |
| 61 | + | ReadLn; | |
| 62 | + | end. | |
| 63 | + | ``` | |
| 64 | + | ||
| 65 | + | With `{$modeswitch classicprocvars+}` the [[@|@-address-operator]] is not necessary to refer to methods. Also, if you are using the `@`-address-operator usage of `{$typedaddress on}` is advised in order to prevent programming mistakes. | |
| 66 | + | ||
| 67 | + | --- | |
| 68 | + | *From the Free Pascal wiki, page "How to use procedural variables", as it stood on 2020-02-17 after a change by Trev, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).* |
