为什么不关闭这个工作? [英] Why isn't this closure working?

查看:73
本文介绍了为什么不关闭这个工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下结局:

  def deepSave = {
it.attributes.each {it.save (validate:false)}
it.elements.each {deepSave(it)}
it.save(validate:false)
}

其中我称之为:

  deepSave(someObject .rootElement)

这个失败了,堆栈跟踪很长,填满了PowerShell的缓冲区,所以我看不到原因是。



我使用下面的方法来做同样的事情,我试图用闭包来处理它,并且工作正常。为什么不能关闭工作?

  private def deepSave(def someElement){
for(someElement中的属性。属性){
attribute.save(validate:false)
}
for(元素在someElement.elements中){
deepSave(element)
}
someElement.save(validate:false)
}


解决方案

我不确定递归方案的有效性,但是当您使用 closure 进行递归时,变量必须在定制之前进行预定义以便它可以在递归过程中了解自己。例如:

  def deepSave 
deepSave = {
it.attributes.each {it.save验证:false)}
it.elements.each {deepSave(it)}
it.save(validate:false)
}
pre>

我无法从递归中找出退出策略,但是如果您可以详细说明上下文并添加一个中断示例以运行,那么我可以添加更多内容。


I have the following closure:

def deepSave = {
    it.attributes.each{it.save(validate: false)}
    it.elements.each{deepSave(it)}
    it.save(validate: false)
}

Which I call like this:

deepSave(someObject.rootElement)

This fails and the stacktrace is so long it fills up PowerShell's buffer so I cannot see what the cause is.

I made the method below to do the same thing I am attempting to do with the closure and it works fine. Why isn't the closure working?

private def deepSave(def someElement) {
    for(attribute in someElement.attributes) {
        attribute.save(validate: false)
    }
    for(element in someElement.elements) {
        deepSave(element)
    }
    someElement.save(validate: false)
}

解决方案

I am not sure about the validity of the scenario where you are doing the recursion but when you use closure for recursion the variable has to be pre-defined before it is tailored so that it can know about itself during recursion. For example:

def deepSave
deepSave = {
    it.attributes.each{it.save(validate: false)}
    it.elements.each{deepSave(it)}
    it.save(validate: false)
}

I could not figure out an exit strategy from the recursion, but I can add some more to it if you can elaborate on the context and add a breaking sample to run.

这篇关于为什么不关闭这个工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆