Our current approach to deferring from one handler to another is to call fwd, which is dynamically bound to the next relevant handler. This puts multiple frames on the stack and for deeply nested handlers (as in weighted) we quickly blow the stack on even moderately sized terms.
However, the common calling pattern for fwd is that the call is in tail position, so in principle there's no need to use any additional stack frames in these cases. Python doesn't offer tail-call optimization, so we would need our own solution.
The simplest solution that I can see is to have another pattern for forwarding where the handler would return a marker value instead of calling fwd. This would have the advantage of forcing use only in tail position, but makes the forwarding interface more complex.
Our current approach to deferring from one handler to another is to call
fwd, which is dynamically bound to the next relevant handler. This puts multiple frames on the stack and for deeply nested handlers (as in weighted) we quickly blow the stack on even moderately sized terms.However, the common calling pattern for
fwdis that the call is in tail position, so in principle there's no need to use any additional stack frames in these cases. Python doesn't offer tail-call optimization, so we would need our own solution.The simplest solution that I can see is to have another pattern for forwarding where the handler would return a marker value instead of calling
fwd. This would have the advantage of forcing use only in tail position, but makes the forwarding interface more complex.