async函数作为hacklang中的类型



我试图建立一个shape,其中关键是字符串和值是功能。这对常规函数很好,但对异步函数会产生错误。以下是我要做的

const type TAbc = shape(
'works' => (function (): string),
'doesnt_work' => (async function(): Awaitable<string>)
);

这会导致错误A type specifier is expected here.Hack(1002)

Encountered unexpected text async, was expecting a type hint.Hack(1002)

这在Hacklang中是非法的吗?如果有,想知道原因吗?

async function (): T在Hacklang中是非法的。建议的方法是在返回类型Awaitable<T>中定义它。

要使它工作,我们可以这样做

const type TAbc = shape(
'sync' => (function (): string),
'async' => (function(): Awaitable<string>)
);

初始化这种类型的实例时,可以在内部调用async函数。如:

$abc = shape(
'async' => function(): Awaitable<string> {
return someAsyncFunction();
});