是否“重置”?需要“转移”在块内?
reset
需要块内的 shift
是否正确?我尝试了一下并得到以下结果:
scala> reset {} error: cannot cps-transform expression (): type arguments [Unit,Unit,Nothing] do not conform to method shiftUnit's type parameter bounds [A,B,C >: B]
它看起来合理(因为内部没有 shift
的 reset
块是“死代码”,永远不会执行)但我不明白该错误。
错误消息的确切含义是什么?
Is it correct that reset
requires shift
inside the block? I tried it and got the following:
scala> reset {} error: cannot cps-transform expression (): type arguments [Unit,Unit,Nothing] do not conform to method shiftUnit's type parameter bounds [A,B,C >: B]
It looks reasonable (since reset
block without shift
inside is "dead code", which is never executed) but I do not understand the error.
What is the exact meaning of the error message?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不同意,如果没有
shift
,reset
中的代码就死了。实际上reset
只是定义了延续的边界(那是因为它们被称为分隔延续)。如果你在某个地方有shift
,代码就会死掉在reset
内并且您不调用延续函数。例如:shift
之后的代码是死的(println(3)
),因为我没有调用k(Unit)
。另一方面,似乎
reset
期望从它的主体中得到一些特殊的返回类型 - 使用@cpsParam
注释进行注释的类型。您可以检查reset
方法的定义:并且
shift
产生的正是reset
方法所期望的。以下是shift
方法的定义:但是您仍然可以使用
reset
而无需在其中调用shift
。这个技巧可以做到这一点:请注意,
@cps
只是@cpsParam
的类型别名。这是它的定义:I don't agree, that code within
reset
is dead withoutshift
. Actuallyreset
just defines the boundaries of a continuation (that's because they are called delimited continuations).The code would be dead if you haveshift
somewhere withinreset
and you do not call continuation function. For example:The code after
shift
is dead (println(3)
) because I have not calledk(Unit)
.From the other hand, seems that
reset
expects some special return type from it's body - the one that annotated with@cpsParam
annotation. You can check definition ofreset
method:And
shift
produces just whatreset
method expects. Here is definition ofshift
method:But you still can use
reset
withoutshift
call within it. This trick will do it:Please note, that
@cps
is just type alias for@cpsParam
. Here it's definition: