Let's say that I have a proc which does something singleton-like, it initializes internal structures and later only executes a different branch of code which depends on those structures. The layout would be something like this:
proc foo() =
if global_not_initialized:
initialize_stuff()
else:
use_initialized_stuff()
Can a proc rewrite itself at runtime? The purpose would be for initialize_stuff() to rewrite the behaviour of foo() to actually embed the contents from use_initialized_stuff() directly, avoiding the boolean check and internal function call. Callers would be oblivious to this internal trickery.Simply make it a procedure variable. E.g.:
var foo: proc()
proc use_initialized_stuff() =
echo "use"
proc initialize_stuff() =
echo "init"
foo = use_initialized_stuff
foo = initialize_stuff
foo()
foo()