Menu

scScript Non-Local Returns

scScript supports an RC data structure that represents the RunContext of a function (or think of it as Remote Control for the function). MyFunc can get access to its own RC using the GetRC: (in sc package)

   Var TheRC = GetRC();

The hypothetical MyFunc can then hand TheRC over to a function (Sub1) that it calls which can hand it to a function (Sub2) that it calls... on and on to SubN. At that point SubN can execute:

   Return X OutOf TheRC;

This will immediately return the value of X to the caller of MyFunc, exactly as if MyFunc itself executed:

   Return X;

This capability has two common uses:

  1. Error-return: Lower functions in a call chain can simply exit with an error code, without every higher function having to test, catch, and propagate error codes.
  2. Recursion: The lower function in a deep traversal can find the solution and return it without having to pop repeatedly back up the chain.

scScript also optimizes tail-recursion, so the lower function in a tail-recursive chain can implicitly return from up the chain (but only if the function does not call GetRC). In these cases, scScript effectively eliminates the recursion itself, so no extra (stack frame) memory is used.

In early versions, the OutOf keyword was given the more succinct and logical name From. But it soon because obvious that From was not a good keyword as it is too common a variable name! Also, the original name for RC, perhaps still visible in source codes, was confusingly RunCall rather than RunContext.