Dfn lexical closures versus tradfn debuggability when error guards must cross paradigm boundaries
0 reputation · 18 Nov 2021, 22:42 UTC
0 reputation · 18 Nov 2021, 22:42 UTC
A new Dyalog APL codebase will mix direct functions (dfns) and traditional functions (tradfns).
The team must adopt a single error‑handling convention and retain the ability to suspend execution during development.
Dfns capture surrounding namespace state lexically at definition time, enabling closures and tail‑call optimization, but their :: error guards only trap errors inside nested dfns, not errors raised by called tradfns. Tradfns provide uniform :: guards that catch errors in both dfns and tradfns and support full line‑by‑line debugging, yet they lack lexical closures and tail‑call optimization. Additionally, dfns cannot be suspended mid‑execution, while tradfns allow full workspace inspection.
Therefore the design decision is whether to standardize on dfns or tradfns for new modules when a uniform error‑handling style and routine debugging suspension are required.
Should new modules default to dfns or tradfns when error handling must be uniform and debugging suspension is required? Is there a pattern to bridge dfn error‑guard asymmetry without wrapping each cross‑paradigm call? Does 18.2 dfn tracing sufficiently improve dfn debuggability for production workflows?
36775 reputation · 19 Nov 2021, 00:41 UTC
Default new modules to tradfn when the team needs one error-handling convention and routine suspension during development. tradfn :: guards are expected to catch errors raised by both dfn and tradfn callees, and tradfn frames can be broken into for workspace inspection. dfn :: guards are likely scoped to errors raised inside the dfn itself and do not reliably trap errors propagated from called tradfn code; dfn code also cannot be suspended mid-execution.
The asymmetry comes from how each paradigm treats the error stack. A dfn closes over its definition-time namespace and supports tail-call optimization, but its :: guard is a local construct. A tradfn guard wraps the whole traditional function frame, so errors thrown by nested dfn calls or called tradfn routines surface at the same :: site. This makes tradfn the safer default for uniform handling, at the cost of lexical closures and tail-call optimization.
These behaviors are version-sensitive. Dyalog APL releases before 17.0 may not handle cross-paradigm :: guards uniformly, so treat the guard descriptions as likely behavior pending verification in the target environment.
Place one tradfn adapter at the module boundary. The dfn calls the sentinel; the sentinel invokes the tradfn work and owns the :: guard. This centralizes guard coverage so individual dfn call sites do not need their own wrappers.
∇ result ← Sentinel args
[1] :: Handle
[2] result ← TargetTradfn args
[3] → 0
[4] Handle:
[5] result ← 'handled'
∇
Maintain this sentinel as a shared boundary component. It adds one interface to review per module evolution, which is cheaper than wrapping every cross-paradigm call.
No, not for the stated requirements. 18.2 dfn tracing improves execution-stack visibility, but it does not make dfn :: guards catch errors from called tradfn code, and it does not add mid-execution suspension to dfn code. Treat tracing as a development aid only; production workflows that require suspension still need tradfn frames or an external break mechanism. Runtime tracing overhead has not been measured here and should be checked before enabling it in production.
This answer assumes Dyalog APL 18.2 or a comparable modern release. The missing diagnostic detail that could change this recommendation is: what Dyalog APL version is the target runtime? If the target predates version 17.0, re-verify :: cross-paradigm coverage before standardizing on tradfn.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.