移位在重置块中的位置重要吗?
假设,有一个带有单个 shift
的 reset
块:
val r = reset { // do smth. 1 shift {...} // do smth. 2 // do smth. 3 }
我将 shift
放在“do smth.2”或“之后是否正确?执行 smth.3" 而不改变结果r
? shift
位于 reset
块中的位置并不重要,这是正确的吗?
Suppose, there is a reset
block with a single shift
:
val r = reset { // do smth. 1 shift {...} // do smth. 2 // do smth. 3 }
Is it correct that I place the shift
after "do smth. 2" or "do smth. 3" without changing the result r
? Is it correct that it does not matter where shift
stands in a reset
block?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这很大程度上取决于您在
shift
中所做的事情。如果您只是像这样调用提供的函数:shift((k: Unit => Unit) => k(Unit))
那么,在您的特定示例中,< code>shift 代表。Shift
函数仅捕获其他函数中紧随其后的代码(在我的示例中,该函数称为k
)。换句话说,这段代码:将被编译器重写为类似这样的内容(这段代码只是演示一般想法,它不应该显示编译器插件实际生成的内容):
但是如果您在
shift,就像条件
k
执行一样,那么这个shift
所在的位置确实很重要。希望这有帮助(我希望我正确理解你的问题)
It highly depends on what you are making within
shift
. If you just calling provided function like this:shift((k: Unit => Unit) => k(Unit))
then, in your particular example, it really doesn't matter whereshift
stands.Shift
function just captures code that comes after it in other function (in my example this function is calledk
). In other words, this code:would be rewritten by compiler in something like this (this code just demonstrates general idea and it's not supposed to show what compiler plugin will actually generate):
But if you have some logic inside
shift
, like conditionalk
execution, then it really matters where thisshift
stands.Hope this helps (and I hope, that I understood your question correctly)
只是添加到已经给出的答案中,您可以移动
shift
的地方是是否在 shift 之前有代码,或者将其放在传递给shift
的函数内:是与
Just adding to the answer already given, the place where you CAN move around
shift
is whether to have code before the shift or have it inside the function you pass toshift
:is the same as