前置技能:TypeScript 基础,续延
有时候我们希望在异常抛出后经过保存异常信息再跳回原来的地方继续执行。
显然 TypeScript 默认异常处理无法直接实现这样的需求,因为在异常抛出时整个调用栈的信息全部被清除了。
但如果我们有了异常抛出时的续延那么可以同时抛出,在 catch
块中调用这个续延就能恢复之前的执行状态。
下面是实现可恢复异常的 try-catch
:
type Runnable = () => void;
const CE: ((e: Error, cout: Runnable) => void)[] = [];
const try_fun = (
body: (cout: Runnable) => void,
handler: (e: Error, cout: Runnable) => void,
cout: Runnable
) => {
CE.push(handler);
body(cout);
CE.pop()
}
const throw_fun = (e: Error, cont: Runnable) => {
CE[CE.length - 1](e, cont);
}
然后就可以像下面这样使用:
const try_run = (t: number) => {
try_fun(
(cout: Runnable) => {
console.log("try");
if (t === 0) {
throw_fun(new Error("t < 0"), () => {
console.log("resumed");
cout();
}
)
}
else {
console.log(100 / t);
cout();
}
},
(e: Error, cont: Runnable) => {
// https://stackoverflow.com/a/38633821/13040423
console.error(e.message);
cont();
}
,
() => {
console.log("end");
}
)
}
而调用 try_run(0)
就会得到:
try_run(0)
try
t < 0
resumed
end
虽然
npm test
会失败,但是运行出来的确顺序是对的。见 playground
如果说在刚刚异常恢复的基础上希望在恢复时修补之前的异常错误就需要把之前的 resume
函数加上参数,这样修改以后它就成了代数作用(Algebaic Effect)的基础工具:
type Runnable = () => void;
type RunWith = (t: number) => void;
const CE: ((e: Error, cout: RunWith) => void)[] = [];
const try_fun = (
body: (cout: Runnable) => void,
handler: (e: Error, cout: RunWith) => void,
cout: Runnable
) => {
CE.push(handler);
body(cout);
CE.pop()
}
const Perform = (e: Error, cont: RunWith) => {
CE[CE.length - 1](e, cont);
}
使用方式如下:
const try_run = (t: number) => {
try_fun(
(cout: Runnable) => {
console.log("try");
if (t === 0) {
Perform(new Error("t < 0"), (v) => {
console.log("resumed");
console.log(100 / v);
cout();
}
)
}
else {
console.log(100 / t);
cout();
}
},
(e: Error, cont: RunWith) => {
// https://stackoverflow.com/a/38633821/13040423
console.error(e.message);
cont(1);
}
,
() => {
console.log("end");
}
)
}
而这个东西能实现不只是异常的功能,从某种程度上来说它能跨越函数发生作用(Perform Effect)。
比如说现在有个函数要记录日志,但是它并不关心如何记录日志,输出到标准流还是写入到文件或是上传到数据库。这时候它就可以调用
Perform(new LogIt(INFO, "test"), ...);
来发生(Perform)一个记录日志的作用(Effect)然后再回到之前调用的位置继续执行,而具体这个作用产生了什么效果就由调用这个函数的人实现的 try
中的 handler
决定。这样发生作用和执行作用(Handle Effect)就解耦了。
进一步讲,发生作用和执行作用是可组合的。对于需要发生记录日志的作用,可以预先写一个输出到标准流的的执行器(Handler)一个输出到文件的执行器然后在调用函数的时候按需组合。这也就是它是代数的(Algebiac)的原因。
细心的读者还会发现这个东西还能跨函数传递数据,在需要某个量的时候调用
Perform(new Ask("config"), ...);
就可以获得这个量而不用关心这个量是怎么来的,内存中来还是读取文件或者 HTTP 拉取。从而实现获取和使用的解耦。
而且这样的操作和状态单子非常非常像,实际上它就是和相比状态单子来说没有修改操作的读取器单子(Reader Monad)同构。
也就是说把执行器函数作为读取器单子的状态并在发生作用的时候执行对应函数就可以达到和用续延实现的代数作用相同的效果,反过来也同样可以模拟。