How would one declare a variable that is to contain a procedure later on?
E.g.: instead of testing a condition over and over again in each run of a proc and branching depending on the condition, I'd create two functions, one for each of the possible values, check the condition once (which is not going to change during some certain interval), and then assign the desired funtion to the container. Later on the container variable holding the proc can be repeatedly called without further checking and branching
My current code is like below. However I'd prefer to declare the container variable first and define the procs later on:
proc a1(par1: int): int = result = something proc a2(par1: int): int = result = a different thing var a = a1 if condition: a = a2 a(intvar) # some code here ... a(intvar2) # some further code here ... a(intvarx)
Would something like this work for you?
proc a(x: int): int = x+1
proc b(x: int): int = x+2
var container: proc(x:int):int
if 4 > 5:
container = a
else:
container = b
echo $container(3)