function findRoutingFor(method, path) { var foundRoute, routeArgs; // some computation here if (foundRoute) return [foundRoute, routeArgs]; } // and at call site var rv = findRoutingFor(method, path); if (!rv) throw new Error(/* */); var foundRoute = rv[0]; var routeArgs = rv[1];
I believe, that I came up with more elegant way to do that. Continuation passing style saves us here. Here's how:
// notice extra parameter function findRoutingFor(method, path, body) { var foundRoute, routeArgs; // same computation here // and then we invoke continuation return body(foundRoute, routeArgs); } // then at call site... return findRoutingFor(method, path, function (foundRoute, routeArgs) { if (!foundRoute) throw new Error(/* */); // rest of call site is here });
So continuation passing style allows us to avoid packing & unpacking of multiple return values. There are some costs, like, for example, extra indentation of rest of call site, but, IMO, in many cases it's better than usual alternative.
No comments:
Post a Comment