开发者

groovy way of if(var) fn(var)

开发者 https://www.devze.com 2023-03-07 02:30 出处:网络
i want to know is there any groovier way of code below: def dataList = OperLog.createCriteria().list(max:params.max, offset:params.offset) {

i want to know is there any groovier way of code below:

def dataList = OperLog.createCriteria().list(max:params.max, offset:params.offset) {
    if(params.relationId){
      eq('relationId',params.long('relationId'))
    }
    order(params.sort, params.order)
}

such as someVar?.someMethod is there any sugar of don't call a method where it's params i开发者_如何学Pythons null


You could do:

params.relationId?.with { rid ->
  println rid
}

And the code inside the with block will not be executed if params.relationId is null...

However, I'd argue that your original code is more obvious in its intentions, and you won't have to try and work out what it is doing when you come to review it at a later date ;-)


is there any groovier style of this?

def list = [vo1,vo2,vo3]
list.each{
   someMethod(it)
}

just like

list*.toString()
0

精彩评论

暂无评论...
验证码 换一张
取 消