Main sections
PROTOTYPE
PROTOTYPE proto: arg1%$ arg2%$ ...
This topic is very complex and thus not recommended for beginners.
A function prototype is an empty function that points to a different function. You call your pointer and it redirects your program to run the underlying function. The advantage in using a prototype instead of just calling the original function is that you can dynamically change the function that the prototype points to. In this way you can call your prototype multiple times with the same parameters, and each time your program may do different things depending on the function it ends up running.
Create a prototype using the command 'PROTOTYPE'.
PROTOTYPE FunctionProto: a
You can then use the prototype as a variable. i.e.
'LOCAL/GLOBAL [variable] AS [prototype name]'.
Example:
PROTOTYPE FunctionProto: a
LOCAL PointerVariable AS FunctionProto
Once the prototype has been created, you point it at a function with the command '[prototype name] = [function name]'
PROTOTYPE FunctionProto: a, b // Create the prototype "FunctionProto" that takes two arguments - a and b.
LOCAL FunctionPointer AS FunctionProto // Create "FunctionPointer" as a variable of type "prototype"
FunctionPointer= MyTestFunction // Point "FunctionPointer" at the function "MyTestFunction"
FunctionPointer(1,2) // Call "FunctionPointer(1,2)" which will actually call "MyTestFunction(1,2)"
FUNCTION MyTestFunction: a,b
ENDFUNCTION
The function pointers can be members of a TYPE:
TYPE MyType
FunctionPointer AS FunctionProto
ENDTYPE
You can use the "IF" command to determine whether a pointer has been assigned to a function. If it has been assigned then the command "IF [prototype]" will return TRUE.
Example:
LOCAL FunctionPointer AS FunctionProto
IF FunctionPointer = FALSE THEN STDOUT "Pointer has NOT been assigned to a function\n"
FunctionPointer = MyTestFunction
IF FunctionPointer = True THEN STDOUT "Pointer has now been assigned to a function\n"
If you use an unassigned function pointer, the program will stop with the error message : "Error: Argument Error-Type or count unsupported."
Prototypes will often come in handy when you have a big switch statement in your code.
Pointer usage example:
PROTOTYPE func_proto: a
TYPE Tfoo
moo AS func_proto
ENDTYPE
LOCAL pFoo AS func_proto
IF pFoo=FALSE THEN STDOUT "has no pointer\n"
pFoo = f1
pFoo(1)
pFoo = f2
pFoo(2)
call(pFoo)
LOCAL tp AS Tfoo
tp.moo = f1
tp.moo(4)
ALIAS fkt AS tp.moo
fkt(5)
KEYWAIT
FUNCTION f1: one
STDOUT "f1: "+one+"\n"
ENDFUNCTION
FUNCTION f2: a
STDOUT "f2: "+a+"\n"
ENDFUNCTION
FUNCTION call: foo AS func_proto
foo(3)
ENDFUNCTION
Output:
has no pointer
f1: 1
f2: 2
f2: 3
f1: 4
f1: 5