从任务中的块中断循环
Break loop from a block in a task
所以我有类似的代码,我想 运行:
main:
// This works
mloop :
print "test"
return
//This will not compile
task:: mloop :
print "test"
return //this seems t be the problem
mloop [block]:
while true:
sleep --ms=100
block.call
我想从块内打破无限循环。但我还需要 运行 任务中的循环。它不会编译并给出错误消息 Can't explicitly return from within a lambda
。这似乎不适用于 return
或 continue
。有什么方法可以实现类似的功能吗?
这是语言当前迭代的一个限制。您可能希望使用 break.mloop
来跳出循环:
main:
task:: mloop:
print "test"
break.mloop // Read as "break out of mloop".
mloop [block]:
while true: ...
但是,此功能尚未在编译器中实现。
您有几种解决方法:
- 在函数中创建传递给任务的 lambda:
task_function:
mloop:
print "test"
return
main:
task:: task_function
这总是有效的。
- 使用方块的 return 值表示退出。
my_mloop [block]:
mloop:
if block.call: return
main:
task:: my_mloop:
print "test"
true // Signal end.
后者的优点是块的代码将在 main
内,因此可以更轻松地共享变量。它仅在 mloop
函数不需要来自给定块的值时才有效。
所以我有类似的代码,我想 运行:
main:
// This works
mloop :
print "test"
return
//This will not compile
task:: mloop :
print "test"
return //this seems t be the problem
mloop [block]:
while true:
sleep --ms=100
block.call
我想从块内打破无限循环。但我还需要 运行 任务中的循环。它不会编译并给出错误消息 Can't explicitly return from within a lambda
。这似乎不适用于 return
或 continue
。有什么方法可以实现类似的功能吗?
这是语言当前迭代的一个限制。您可能希望使用 break.mloop
来跳出循环:
main:
task:: mloop:
print "test"
break.mloop // Read as "break out of mloop".
mloop [block]:
while true: ...
但是,此功能尚未在编译器中实现。
您有几种解决方法:
- 在函数中创建传递给任务的 lambda:
task_function:
mloop:
print "test"
return
main:
task:: task_function
这总是有效的。
- 使用方块的 return 值表示退出。
my_mloop [block]:
mloop:
if block.call: return
main:
task:: my_mloop:
print "test"
true // Signal end.
后者的优点是块的代码将在 main
内,因此可以更轻松地共享变量。它仅在 mloop
函数不需要来自给定块的值时才有效。