Reliable clean-up in Mathematica

后端 未结 3 1859
迷失自我
迷失自我 2020-12-24 08:52

For better or worse, Mathematica provides a wealth of constructs that allow you to do non-local transfers of control, including Return, Catch/Throw, Abort and Goto. However,

3条回答
  •  离开以前
    2020-12-24 09:45

    Pillsy's later version of CleanUp is a good one. At the risk of being pedantic, I must point out a troublesome use case:

    Catch[CleanUp[Throw[23], Print["cleanup"]]]
    

    The problem is due to the fact that one cannot explicitly specify a tag pattern for Catch that will match an untagged Throw.

    The following version of CleanUp addresses that problem:

    SetAttributes[CleanUp, HoldAll]
    CleanUp[expr_, cleanup_] :=
      Module[{exprFn, result, abort = False, rethrow = True, seq},
        exprFn[] := expr;
        result = CheckAbort[
          Catch[
            Catch[result = exprFn[]; rethrow = False; result],
            _,
            seq[##]&
          ],
          abort = True
        ];
        cleanup;
        If[abort, Abort[]];
        If[rethrow, Throw[result /. seq -> Sequence]];
        result
      ]
    

    Alas, this code is even less likely to be competitive in a beauty contest. Furthermore, it wouldn't surprise me if someone jumped in with yet another non-local control flow that that this code will not handle. Even in the unlikely event that it handles all possible cases now, problematic cases could be introduced in Mathematica X (where X > 7.01).

    I fear that there cannot be a definitive answer to this problem until Wolfram introduces a new control structure expressly for this purpose. UnwindProtect would be a fine name for such a facility.

提交回复
热议问题