The return value is represented inside the body of a routine as the special result variable. This allows for a mechanism much like C++’s “named return value optimization” (NRVO). NRVO means that the stores to result inside p directly affect the destination dest in let/var dest = p(args) (definition of dest) and also in dest = p(args) (assignment to dest). This is achieved by rewriting dest = p(args) to p’(args, dest) where p’ is a variation of p that returns void and receives a hidden mutable parameter representing result.

    Let T’s be p’s return type. NRVO applies for T if sizeof(T) >= N (where N is implementation dependent), in other words, it applies for “big” structures.

    1. BigT = array[16, int]
    2. proc p(raiseAt: int): BigT =
    3. for i in 0..high(result):
    4. result[i] = i
    5. proc main =
    6. var x: BigT
    7. except ValueError:
    8. doAssert x == [0, 1, 2, 3, 4, 5, 6, 7, 0, 0, 0, 0, 0, 0, 0, 0]

    However, the current implementation produces a warning in these cases. There are different ways to deal with this warning:

    1. Disable the warning via {.push warning[ObservableStores]: off.} … {.pop.}. Then one may need to ensure that p only raises before any stores to result happen.