Search code examples
code-generationrebolred-lang

Is it possible to generate function dynamically in rebol or red


Instead of hard coding (contrived example here):

    f1: func[][print "f1"]
    f100: func[][print "f100"]
    etc...

Is it possible to do something like this

    for num 1 100 1 [ 
      set to-word rejoin ["f" num] func[][
        print rejoin ["f" num]
      ]
    ]

except it doesn't work, it always give "f100" (why in fact num is equal to 100 is a mystery for me though it's not the question here: closure ?).


Solution

  • for num 1 100 1 [ 
      set to-word rejoin ["f" num] func[] compose/deep [
        print rejoin  ["f"  (num)]
      ]
    ]
    
    >> f1
    f1
    >> f2
    f2
    >> f99
    f99
    

    The result of your functions is always f100, because they all share the same word num with the outcome of your for loop. You can see that if you do e.g.source f1.