MNie
MNie

Reputation: 1367

Return specific value from grails closure

I have a problem with returning specific value from grails closure in my case it's ArrayList.

Here is my code:

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
}

and I'm executing this like : fun(list1, 10) or fun(list1) where list1 contains some elements.

Also when I debug my code I find out that my closure returning value type is an Event.. And I don't have any idea, what I'm doing wrong, of course if it's legal to do something like this.

I also try specific type of my closure to ArrayList but this throw an error that closure cannot be converted to ArrayList. I will be very grateful for help!

Upvotes: 1

Views: 67

Answers (2)

injecteer
injecteer

Reputation: 20699

the calls are equivalent. Also, I wouldn't mess with optional arguments that much. I'd put it like:

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

Upvotes: 0

MNie
MNie

Reputation: 1367

I found where I have made my mistake, to call a closure instead of:

def res = fun(list1, 10)

It should be:

def res = fun.call(list1, 10)

And everything works fine for me :)

Upvotes: 1

Related Questions