Why isn't this closure working?

前端 未结 1 1806
时光取名叫无心
时光取名叫无心 2021-01-26 13:03

I have the following closure:

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


        
1条回答
  •  感情败类
    2021-01-26 13:26

    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.

    0 讨论(0)
提交回复
热议问题