从grails闭包返回特定值



我遇到了从grails闭包返回特定值的问题,在我的例子中是ArrayList。

这是我的代码:

def fun=
{
    list1, limit = list1.size()-1 ->
    def returnList = new ArrayList()
    for(Elem el in list1)
    {
        def info = el.getInfo()
        boolean toAdd = true
        if(info.size() <= 1)
        {              
           aut.each
           {
               icz ->
                   if(icz.info == "hehe")
                   {
                       toAdd = false
                   }
           }
        }
        if(toAdd)
        {
            returnList.add(el)
            --limit
        }
        if(limit < 0)
        {
            break
        }
    }
    returnList
}

我执行的是:fun(list1,10)或fun(list 1),其中list1包含一些元素。

此外,当我调试代码时,我发现我的闭包返回值类型是Event。。我不知道我做错了什么,当然,如果这样做是合法的。

我也尝试将特定类型的闭包转换为ArrayList,但这引发了一个错误,即闭包无法转换为ArrayNist。我将非常感谢你的帮助!

我发现了我犯错误的地方,调用闭包而不是:

def res = fun(list1, 10)

应该是:

def res = fun.call(list1, 10)

一切对我来说都很好:)

这些调用是等价的。此外,我也不会那么多随意的争论。我会这样说:

def fun = { list1, limit = null ->
  if( null == limit ) limit = list1.size() - 1
  ...
}

最新更新