为什么promise不是Scheme中的数据类型?
在Scheme中delay
返回的对象是“一个promise”,但是promise不被认为是一种类型(所以没有promise?
过程,并且它没有被列为R5RS 或 R6RS 中的一种类型)。
为什么会这样?有强烈的理由吗?例如,对我来说,做类似 (if (promise? x) (force x) x)
的事情似乎很自然。 (我发现有些实现会让我强制执行非承诺,而其他实现则不会)。另外,如果我可以将某些内容存储在变量中并将其传递,我觉得它应该有一个类型。
The object returned by delay
in Scheme is "a promise", but promises are not considered to be a type (so there is no promise?
procedure, and it's not listed as a type in R5RS or R6RS).
Is there a strong reson why this is so? It would seem quite natural to me to do something like (if (promise? x) (force x) x)
, for example. (And I see that some implementations will let me force non-promises, and others will not). Also, if I can store something in a variale and pass it around, I feel like it should have a type.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不可能有那么有力的理由,因为 MIT/GNU 方案,定义了一个
promise?
函数。There can't be that strong a reason, since MIT/GNU scheme, defines a
promise?
function.我认为它可以更优化地实现延迟/强制。事实上,强制值可以被记忆(这样一个 Promise 只被强制一次,结果值会在后续的 Force 调用中返回)模糊了 Promise 和它的结果值之间的区别。如果你有
promise?
你就不能在任何需要它的地方用它的值来代替强制的promise。因此,根据实现的不同,promise 可能与任何其他 Scheme 值无法区分。I think it allows for a more optimized implementation of
delay/force
. The fact that the forced value can be memoized (so that a promise is really forced only once and the resulting value is returned on subsequentforce
calls) blurs the distinction between a promise and its resulting value. If you havepromise?
you cannot substitute a forced promise by its value everywhere it is needed. Therefore, depending on the implementation, a promise can be indistinguishable from any other Scheme value.