Do you just store the function call or do you also store the parameters 
themselves? If so, why not store a closure instead?
    
    
    proc doubleIt(x: int): int = x*2
    
    let someInt = expensiveComputation()
    proc myClosure(): int = myProc(someInt)
    
    
    Run

Now you can execute `myClosure` wherever you want and it'll execute `myProc` 
with the result from `expensiveComputation`.

You can also store this closure in an object if that's nicer for you:
    
    
    type Store[T] = object
      clos: proc(): T {.closure.}
    
    let store = Store(clos: myClosure)
    store.clos()
    
    
    Run

However, this means that not all stores are interchangeable. You can't store a 
closure that returns a string in a Store has the type `Store[int]`.

In those cases you _maybe_ can store the pointer, like so:
    
    
    type Store = object
      command: pointer
    
    let s = Store(command: proc(): int = 5)
    
    
    Run

Now you can store multiple Stores in a Seq or whatever you need. This comes 
with 2 limitations:

  * It is impossible to store closures, so you need to have the parameters 
inside of `Store` in one way or another
  * This absolutely **requires** you to know which type `command` will return 
when you want to execute the proc, because that needs to look like so:


    
    
    let s = Store(command: proc(): int = 5)
    
    echo cast[proc(): int {.nimcall.}](s.command)()
    
    
    Run

There are other solutions as well depending on your need, but those tend to 
dive into metaprogramming, so macros and templates.

Reply via email to